Professional Documents
Culture Documents
Range
byte
8-bit, signed
-128 to 127
short
16-bit, signed
-32768 to 32767
int
32-bit, signed
-2147483648 to 2147483647
long
64-bit, signed
float
32-bit
double 64-bit
char
boolean
-9223372036854775808 to
9223372036854775807
1.40239846e-45 to 3.40282347e+38
4.94065645841246544e-324 to
1.79769313486231570e+308
Java Operators
Precedence
Operator
Description
++, --
+, -
Bitwise complement
Boolean
*, /, %
+, -
String
<<
Left shift
>>
>>>
Numeric comparison
instanceof
Type comparison
==, !=
==, !=
&
Bitwise AND
Bitwise XOR
Bitwise OR
10
&&
Conditional AND
11
||
Conditional OR
12
?:
13
= *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=
Assignment
Keywords
abstract
default
goto
null
synchronized
boolean
do
if
package
this
break
double
implements
private
throw
byte
else
import
protected
throws
case
extends
instanceof
public
transient
catch
false
int
return
true
char
final
interface
short
try
class
finally
long
static
void
const
float
native
super
volatile
continue
for
new
switch
while
Comments
Java supports three styles of comments:
A standard C-style comment, where all of the characters between
/* and */ are ignored.
A single-line comment, where all of the characters from
// to the end of the line are ignored.
A documentation comment that begins with
/** and ends with */.
Literals
Integer literals
Integer literals can be specified in octal (base 8), decimal (base 10), or
hexadecimal (base 16).
int i = 1230;
int i = 01230; // i = 664 decimal
int i = 0xFFFF;
Floating-point literals
Floating-point literals are of type double unless they are
suffixed with an f denoting that they are to be produced
as a float value:
double d = 8.31;
double e = 3.00e+8;
Character literals
A literal character value can be specified either as a single-quoted
character or as an escaped ASCII or Unicode sequence:
char a = 'a';
char newline = '\n';
char ch = 88 ; //code for X
Boolean Literals
It can have only one of two possible values, true or false
boolean b = false;
Casting
1) int a;
a = 10;
byte b;
b = (byte) a;
2) byte b;
int i = 257;
b = (byte) i; // 1
3) double d = 223.22;
int i = (int) d;
// 223
valid
HelloWorld.java
public class HelloWorld
{
}
C:> javac HelloWorld.java
//produces the HelloWorld.class
C:> java HelloWorld
//when you invoke
the
interpreter, you do not supply the .class
class P
{
public static void main(String args[])
{
int x =34;
int y =42;
System.out.println("X = " + x);
System.out.println("y = " + y);
int z = x+y;
System.out.println("z = " + z);
}
}
All local variables has to be initialized before using.
35
>>2
0000 1000
--------------------------------------------------11111000
-8
>>1
11111100
-4
int a = -1 ;
a = a >>> 24;
11111111 11111111 11111111 11111111
// -1 in binary
>>> 24
00000000 00000000 00000000 11111111 // 255 in binary
Logical AND
Logical OR
Logical XOR
&&
||
If you use the || and && forms rather than | and & forms , Java
will not bother to evaluate the right hand operand when the outcome of
the expression can be determined by the left operand alone.
if( denom != 0 && num /denom >10)
double x, y, r;
public Circle(double x, double y, double r)
{
this.x = x;
this.y = y;
this.r = r;
}
}
With this new constructor the initialization becomes part of the
object creation step:
Circle c = new Circle(1.414, -1.0,1 .25);
this Again
--it can be used from a constructor to invoke one of the other
constructors of the same class.
public Circle(double x, double y, double r)
{ this.x = x; this.y = y; this.r = r; }
Default constructors
As mentioned previously, a default constructor is one without
arguments.
If you create a class that has no constructors, the compiler will
automatically create a default constructor for you
However, if you define any constructors, the compiler will not
synthesize one for you:
class Bush {
Bush(int i) { }
}
Now if you say:new Bush();
the compiler will complain that it cannot find a constructor that
matches
Class Variables
Java uses the static keyword to indicate that a particular variable
is a class variable rather than an instance variable.
That is, that there is only one copy of the variable exists
regardless of the number of instances of the class that are created
It exists and can be used even if the class is never actually
instantiated.
Static Variable Example
public class Circle
Besides the static keyword that we've already seen, we use the final
keyword, which means that this variable can never have its value
changed.
static Methods
Class methods are like class variables in a number of ways:
Class methods are declared with the static keyword.
Class methods are invoked through the class rather than
through an instance.
Class methods are the closest Java comes to "global"
methods. Because they must be referred to by the class
name, there is no danger of name conflicts.
Class methods differ from instance methods in one
important way: they are not passed an implicit this
reference.
}
// A class method. Returns the bigger of two circles.
public static Circle bigger(Circle a, Circle b)
{
Object Finalization
Just as a constructor method performs initialization for an
object, a Java finalizer method performs finalization for an
object.
Garbage collection automatically frees up the memory
resources used by objects.
But objects may hold other kinds of resources, such as file
descriptors or sockets, as well.
The garbage collector can't free these resources up for you, so
you should write a finalizer method that takes care of things
like closing open files, terminating network connections, and
so on.
Friendly or package-private
The default access has no keyword, but it is commonly referred
to as friendly.
It means that all the other classes in the current package have
access to the friendly member, but to all the classes outside of this
package the member appears to be private.
public protected
package
private
Same class
yes
yes
yes
yes
subClass in same
yes
yes
yes
no
yes
yes
no
no
yes
no
no
no
package
Subclass in
different package
Non-subclass,
different package
Examples on
Objects as arguments
Object Reference
String & StringBuffer class
int i;
class B extends A
{
int j;
// B is a subclass of A
class I1{
public static void main(String args[])
{
B ob = new B();
ob.i=10;
ob.j=20;
class Rect
w=x;
h=y;
}
int area()
{
return w*h;
void disp()
{
System.out.println("Width =" +w +", Height = " + h + "Area ="
+area());
}
}
int d;
void set(int x,int y,int z)
{
set(x,y);
d=z;
}
void put()
{
disp();
System.out.println("Depth =" + d);
}
}
class I3
{
public static void main(String args[])
{
Box ob = new Box();
ob.set(10,20,15);
ob.put();
}
}
Constructor Chaining
class A
{
int i;
public A()
{ // Implicit call to super(); here.
i = 3; }
}
class B extends A
{
// Default constructor:
public B() { super(); }
}
More on super
Shadowed Variables
class A
int x=10;
class B extends A
int x=20;
class C extends B
int x=30;
void disp()
{
// Variable x in class C.
System.out.println("x in C is :" + x);
// Variable x in class B.
System.out.println("x in B is :" + super.x);
System.out.println("x in B is :" + ((B)this).x);
// Variable x in class A.
System.out.println("x in A is :" + ((A)this).x);
class A {
int i = 1;
int f() { return i; }
}
class B extends A
{
int i = 2; // Shadows variable i in class A.
int f() { return -i; } // Overrides method f in class A.
}
public class override_test
{
Polymorphism
Circle c = new
Circle();
Triangle t = new
Triangle();
Line l = new Line();
doStuff(c);
doStuff(t);
Dynamic binding
You can also use the abstract keyword to describe a method that
hasnt been implemented yet as a stub indicating here is an
interface function for all types inherited from this class, but at this
point I dont have any implementation for it.
Pure inheritance
That is, the base class can receive any message you can send to
the derived class because the two have exactly the same
interface.
All you need to do is upcast from the derived class and never look
back to see what exact type of object youre dealing with.
Everything is handled through polymorphism