Java=C++--(C plus plus minus minus),suggesting that Java is C++ with the unnecessary hard parts removed ,and therefore a much simpler language . You will see that many parts are simpler ,and yet in other ways Java isn’t much easier than C++
Operators
You can’t use a non-boolean as if it were a logical expression as you can in C and C++.
eg: int a =1;
int b=2;
System.out.println("a || b is "+(a||b));
Be aware, however ,that the comparison of floating point numbers is very strict. A number that is the tiniest fraction different from another number is still “not equal.“ A number that is the tiniest bit above zero is still nonzero.
If you are used to thinking in terms of e as the base of natural logarithms, you must do a mental translation when you see an expression such as 1.39 e-43f in java;it means 1.39*10-43
If you shift a char, byte ,or short ,it will be promoted to int before the shift takes place, and the result will be an int . Only the five low-order bits of the right-hand side will be used .This prevents you from shifting more than the number of bits in an int. If you’re operating on a long ,you’ll get a long result .Only the six low-order bits of the right-hand side will be used ,so you can’t shift more than the number of bits in a long.
Shifts can be combined with the equal sign(<<= or >>= or >>>=)
Although goto is a reserved word in Java ,it is not used in the language; Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords .It’s not a jump but rather a way to break out of an iteration statement. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism: a label.
It’s important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level.
Initialization & Cleanup
While you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.
The compiler won’t let you call a constructor from inside and method other than a constructor.
Neither garbage collection nor finalization is guaranteed. If the JVM isn’t close to running out of memory, then it might not waste time recovering memory through garbage collection.
public class StaticInitialization {
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Ctreating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table=new Table();
static Cupboard cupboard=new Cupboard();
}
class Bowl{
Bowl(int marker){
System.out.println("Bowl("+ marker+")");
}
void f1(int marker){
System.out.println("f1("+marker+")");
}
}
class Table{
static Bowl bowl1=new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker){
System.out.println("f2("+marker+")");
}
static Bowl bowl2=new Bowl(2);
}
class Cupboard{
Bowl bowl3=new Bowl(3);
static Bowl bowl4=new Bowl(4);
Cupboard(){
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int marker){
System.out.println("f3("+marker+")");
}
static Bowl bowl5=new Bowl(5);
}
output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Ctreating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
From the output ,you can see that the static initialization occurs only if it’s necessary. If you don’t create a Table object and you never refer to Table.bowl1 or Table.bowl2,the static Bowl bowl1 and bowl2 will never be created. They are initialized only when the first Table object is created(or the first Static access occurs).After that, the static objects are not reinitialized.