Basics Flashcards

1
Q

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

A

use in code?
operators &raquo_space; &laquo_space; ! ~ &raquo_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)

&laquo_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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what if i want to check (true or false) if my object belong to a specific class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the relation between expression lvalue and sideeffects

is this : ++i++ valid or not ? and why ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

jshell> 7 % 3 ==> ?
jshe11> -7 % 3 ==> ?
jshe11> 7 % 3 ==> ?
jshe11> -7 % -3 ==> ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

== != (used with primitive datatype not with reference type like class)
what to use for reference type ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

equals(Stringbuilder str)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is object class?

A

object class is the base class of all classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is java.util

A

it is a package which contains a classes and interfaces for performing various functionalities and has Collection framework

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is collection?

A

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly