Skip to main content
Origin of Java 
• World War-2 need of a platform independent 
language 
• Green by Sun Micro Systems comes up 
• Fails due to marketing issues et al 
• Analog-to-Digital Transit, Computer H/W 
advancement, Concept of World Wide Web and 
Internet, Need of security 
• Green transformed to Oak 
• 1994 Java makes official release 
sohamsengupta@yahoo.com1
Three Editions of Java 
• J2SE (Java 2 Standard Edititon) 
• J2EE (Java 2 Enterprise Edition) 
• J2ME (Java 2 Micro Edition) 
sohamsengupta@yahoo.com2
Java Data Types 
Numeric: 1. Whole Numbers 
sohamsengupta@yahoo.com3 
2. Fractions 
Whole Numbers: byte 1 byte default value 0 
short2 bytes default value 0 
int  4 bytes default value 0 
long 8 bytes default value 0 
Fractions: float 4 bytes default value 0.0f 
double 8 bytes default value 0 .0 
Symbolic: char  2 bytes (Sun Unicode Character) default 
valueā€˜u0000’ 
Logical : boolean 1 byte default value false 
User Defined: class and interface (to be described later on)
Your First Java Program 
• In C consider the code snippet… 
#include<stdio.h> 
void main(){ 
printf(ā€œHello From Sohamā€); 
sohamsengupta@yahoo.com4 
}• 
The corresponding Java Code would be… 
class A 
{ 
public static void main(String[] args){ 
System.out.print(ā€œHello from Sohamā€); 
}}
Before I say more on Java some Do’s 
Soham was writing a program as on LHS. After typing out 1000 lines he 
felt he needed an extra statement in if branch, but poor Soham! What he 
did was 
sohamsengupta@yahoo.com5 
• if(condition) 
statement-1; 
statement-2; 
. 
. 
. 
Statement-1000; 
• if(condition) 
statement-1; 
ssttaatteemmeenntt--11AA;; 
statement-2; 
. 
. 
. 
Statement-1000
Poor Me! I intended something else 
I was supposed to type… 
• if(condition){ 
sohamsengupta@yahoo.com6 
statement-1; 
ssttaatteemmeenntt--11AA;; 
}} 
statement-2; 
. 
. 
. 
statement-1000 
• Moral: 
1. When you come across some 
if-else branch, or for, while, 
switch, or any method or 
block, at once type out the 
braces and then carry on with 
your code 
2. It’ll not only save you from 
my condition, but also it’ll 
save you quite a lot of time of 
compilation errors saying… 
ā€œ } requiredā€
More on Java 
Answers to some FAQ about Java 2 
• A java file is saved in .java extension 
• A java file, if successfully compiled, produces x+y number of .class 
files where x and y are the number of classes and interfaces in that 
file 
• In java, you can’t put anything except comments outside a class or 
sohamsengupta@yahoo.com7 
interface block 
• To run java program you need Java Virtual Machine(JVM) which 
comes as a part of JDK. 
• Successful compilation generates .class files which contain byte 
codes that is interpreted by JVM. So, java development generally 
uses both compiler and interpreter. 
• Java Byte codes are platform independent but JVM is different for 
different Operating Systems.
Features of Java 2 (J2SE 1.4) 
1. Platform independent…means runs on any HW+OS environment 
2. Java was developed using C++ but excludes the features like 
sohamsengupta@yahoo.com8 
pointers. 
3. Java is a purely typed language as it does not allow automatic type 
demotion (More on this later on) 
4. Java is object oriented, secure due to its various security features, 
reusable and portable,form-free and case sensitive and supports 
general logical statements like C/C++ 
5. Java is not only platform friendly but also is very much developer 
friendly. As support of this statement, Java does not have concept of 
garbage value. It will never let the program perform read operation 
on a non initialized local variable .Also, it saves you from getting 
tampered data due to overflow and/or underflow since it doesn’t 
allow automatic type demotion
Java Source File Name & Class Name 
1. A java source file may contain any number of classes and interfaces 
and there is no such hard-and-first rule that file name and class/ 
interface name should have relation…. But wait friends, this is 
applicable as long as the file contains no public class/interface. 
More concisely, a java source file must have the same name as that 
of the public class/interface in it. It’s thus implied that a java source 
file can contain one and only one public class/interface 
2. To compile a Java file, say, A.java you have to give the command 
sohamsengupta@yahoo.com9 
…>javac A.java 
3. If successful compilation occurs, you will surely want to run it. And 
your command is going to be …> java MyClass where MyClass is 
the class inside A.java that has then main method. 
4. Remember, MyClass need not always have the main method.
My Second Java Program 
#include<stdio.h> 
void main() 
{ 
int x=940; 
printf(ā€œU scored good marksnā€); 
printf(ā€œYour marks is %dā€,x); 
sohamsengupta@yahoo.com10 
} 
class A 
{ 
public static void main(String[] ar) 
{ 
int x=940; 
System.out.println(ā€œU scored good 
marksā€); 
System.out.print(ā€œYour marks is ā€+x); 
} 
}
Printing an output on the console 
• We generally use the syntax System.out.print() or System.out.println() 
to output text on the console. Difference between them is that println() 
can be used with no arguments where as print() can’t be used without 
an argument. Also, println() automatically appends a trailing new line 
character (ā€˜n’) 
• Don’t ask me more about System.out because my plan is to climb up 
the Java tree step by step and I don’t want to stumble down the stairs. 
Yet, FYI, System is class under java.lang package and out is an static 
object belonging to System class and of type java.io.PrintStream. 
• Note the syntax: System.out.print(ā€œYour marks is ā€+x); 
• Here + is concatenation operator instead and apart 
from being the traditional addition operator. 
sohamsengupta@yahoo.com11
Dual Nature of + operator 
Look at the code snippet below and see the outputs 
int x=9; int y=6; 
15 
System.out.println(x+y); 
int x=9; int y=6; 
System.out.println(ā€œSum is ā€+x+y); 
Sum is 96 
int x=9; int y=6; 
System.out.println(ā€œSum is ā€+(x+y)); 
Sum is 15 
int x=9; int y=6; 
System.out.println(ā€œProduct is ā€+x*y); 
Product is 54 
int x=9; int y=6; 
System.out.println(x+y+ ā€œ is the sumā€); 
15 is the sum 
int x=9; int y=6; 
System.out.println(x-y+ ā€œ is differenceā€); 
3 is difference 
int x=9; int y=6; 
System.out.println(ā€œDifference isā€+x-y); 
Compilation error: 
Operator – cannot be applied to 
java.lang.String,int 
sohamsengupta@yahoo.com12
From the Experts’ Desk 
1. Since you can’t always afford to remember all the precedence rules and 
this sort of things, the Java Guru recommends that you should always 
use parentheses while using arithmetic expressions in the simplest way 
2. As you are mostly accustomed to C/C++, first you’ll ask for a 
counterpart of scanf() and cin>>. Yes. You can take input from console 
through keyboard. But as I’ve told you, wait till I make you climb to 
that level! Java, unlike C/C++ is not meant to be used as a mere 
programming language with console as you did with Turbo C++, 
generating Pascal triangles or Fibonacci Series et al. 
3. Today java is more of a technology than of a language itself. Java can 
carry out robust networking, enterprise web development to small 
mobile device programming. 
4. Note that Java handles all inputs as String, unlike C then tries to 
convert to the intended type. 
sohamsengupta@yahoo.com13
Java As a Typed language: code snippet 
sohamsengupta@yahoo.com14 
byte x=9; 
System.out.println(x); 
Here output will be 9 It’s OK to assign a value to 
a type within range 
byte x=129; 
System.out.println(x); 
Error: Possible loss of 
precision: found int , 
required byte 
Value beyond range. Range 
of byte –128 to +127 
byte x=(byte)129; 
System.out.println(x); 
Output: -127 (data 
tampered due to overflow) 
Explicit type casting may 
cost you to worry later 
int y=5; byte x=y; 
System.out.println(x); 
Error: Possible loss of 
precision: found int , 
required byte 
Though value is in range, 
type int can’t be 
automatically demoted. 
int y=5; byte x=(byte)y; 
System.out.println(x); 
OK. Output: 5 Explicit type demotion ok 
here but can cause OF/UF 
long y=8; int x=y; 
System.out.println(x); 
Guess yourself Automatica type demotion 
not allowed so error. 
float x=9.8; // Error 
Should be float x=9.8f; 
Error: possible loss of 
precision: found double 
required float 
9.8 is double and 9.8f/9.8F 
is float. This is because java 
is memory efficient
Java A Typed Language: Contd. 
1. boolean is the only data type that can’t be converted to 
any type nor any other type be converted to boolean. 
2. Implicit type demotion is not allowed in java. You have 
to do it explicitly. But before that make sure that it 
causes no overflow and/or underflow or both. 
3. Although char is 2 bytes and so is short, yet they are not 
compatible. char is automatically converted to int or 
higher. For hierarchy consult any standard book. 
4. char is unsigned strictly in java and note the following. 
char ch=-90; // Error 
char ch=90; System.out.println(ch); // output : Z 
char ch=90; System.out.println((int)ch);// output 90 
sohamsengupta@yahoo.com15
Operators: Unary & Binary 
1. Rule for Unary Operator: 
ā€œIf the operand is of a type which is below int in the hierarchy, the 
output is converted to int, else the type of the output is the type of 
the operand.ā€ 
2. Rule for Binary Operator: 
ā€œ If the Binary operator takes 2 operators one of type T1 and the 
other of type T2, and max(T1,T2) is less than or equal to int, the 
output is converted to int itself, else the output is of type 
max(T1,T2)ā€ 
This rule is not applicable to increment and decrement operators 
Hold your breath dears! Lots of surprises await you in the next 
slide. 
sohamsengupta@yahoo.com16
Look at the code snippets below 
sohamsengupta@yahoo.com17 
byte x=9; x=-x; 
System.out.println(x); 
Error: Possible loss of 
precision found: int 
required: byte 
Rule-1: inputbyte 
Output int 
byte x=9; byte y=x+1; 
System.out.println(x); 
Same Error Rule:2 i/p byte, int 
O/p int 
byte x=7; byte y=2; 
short z=x+y; 
Same Error! Apply Rule-2 
long x=89; byte y=8; 
int z=x+y; 
Error! Apply Rule-2. Output is 
of type long 
byte x=9; x=x+8; Error Apply Rule-2: Output is 
of type int 
byte x=9; x+=8; x++; OK Rule-2 Not applicable 
for += and ++ operator 
char ch=ā€˜%’; int x=100 
System.out.println(x+ch 
+ ā€œ pure am Iā€); 
Output 137 pure am I Be careful. To avoid 
this, use parentheses or 
String ch=ā€œ%ā€;
Note the Following 
• byte x=ā€˜c’; // is OK 
• int x=12; 
byte y=x; // Error 
• final int x=12; byte y=x; // IS OK 
• final int x=134; byte y=x; // Error out of range 
• This holds only when source data is less than or 
equal to int. 
• Adding the keyword ā€œfinalā€ before a variable 
makes it constant. It can’t be changed and any 
code to change this will result in compilation error 
sohamsengupta@yahoo.com18
Note the following code snippets 
public static void main(String[] args){ 
int x; 
x++; 
System.out.println(x); 
} 
Error: variable 
x might not 
have been 
initialized. 
In Java there is no concept 
of garbage value. Without 
initializing a local variable 
u can’t perform read 
operation on it. 
sohamsengupta@yahoo.com19 
int x; 
if(6>4){x=8; } 
System.out.println(x); 
OK. Output will 
be 8 
6>4 is evaluated at compile 
time. So, since it’s true 
always, x must be 
initialized. 
int x; 
if(6<4){ x=8;} 
System.out.println(x); 
Error:variable 
x might not 
have been 
initialized. 
6<4 is evaluated at compile 
time. So, since it’s false 
always, x must not be 
initialized. 
Replace numerics by variables say, a & 
b. Observe the result, it’s error 
if(a>b){ x=8;} System.out.print(x); 
But making them final will be ok if a>b 
is true 
Error:variable 
x might not 
have been 
initialized. 
a>b or a<b is not evaluated 
during compilation time. 
So, it’s not sure if x is 
initialized or not. Hence 
Error