Basics Flashcards
what should i do if i want to double the value without using multiplication ?
what should i do if i want to half the value without using division
what is the symbol of bitwize not operrator how does it work give eg
logical not operator
use in code?
operators »_space; «_space; ! ~ »_space;> code them
» halves the value
(odd negative approximates to next and odd positive approximates to privious eg -9»1=-5 and 9»1 = 4 )
(Java, integer division rounds toward negative infinity)
«_space;doubles (times)
4»1= 8
4»2=16
! true to false and false to true
~x = -(x+1) using 2s compliment
> > > right shifts irrespective to negative or positive 7»>1
0000 0111 -> 1111 1000 -> 0111 1100
what if i want to check (true or false) if my object belong to a specific class
if (animal instanceof Dog)
The instanceof operator in Java is used to check if an object is an instance of a specific class or implements a particular interface. It returns a boolean value (true or false).
Key Points:
Syntax: object instanceof ClassName
Returns:
true if the object is an instance of the specified class or implements the interface.
false otherwise.
what is the relation between expression lvalue and sideeffects
is this : ++i++ valid or not ? and why ?
The result of ++i is a value, not a variable.
The postfix increment (i++) expects a variable, but ++i already returns a value, not a variable.
This leads to a situation where the compiler doesn’t know how to apply i++ to the result of ++i
jshell> 7 % 3 ==> ?
jshe11> -7 % 3 ==> ?
jshe11> 7 % 3 ==> ?
jshe11> -7 % -3 ==> ?
jshell> 7 % 3 ==> 1
jshe11> -7 % 3 ==> -1
jshe11> 7 % -3 ==> 1
jshe11> -7 % -3 ==> -1
What you’ll notice is that the remainder operator takes a sign which comes from the left hand operand and completely ignores the right hand operand’s sign.
== != (used with primitive datatype not with reference type like class)
what to use for reference type ?
Objects.equals(Object a, Object b)
or create a custom class .equals that overrides actual equals class
There are lots of situations where the .equals method is not overridden that you might expect it would be. A classic example is the StringBuilder, which does not override that. In general, mutable data types in Java’s API usually do not override the equals method.
equals(Stringbuilder str)
The StringBuilder class doesn’t override the equals() method because it is designed for mutable sequences of characters, and typically, reference equality (whether two objects are the same in memory) is more useful in most scenarios.
For comparison of the contents of two StringBuilder objects, you would need to use the toString() method, which converts the StringBuilder to a String.
what is object class?
object class is the base class of all classes
what is java.util
it is a package which contains a classes and interfaces for performing various functionalities and has Collection framework
what is collection?
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.