Professional Documents
Culture Documents
KCS-602 Lecture02 Cse
KCS-602 Lecture02 Cse
KCS-602 Lecture02 Cse
HISTORY OF JAVA
• Java is developed by SUN Microsystems in 1991.
• Created by James Gosling, Patrick Naughton,,
Chris worth, Ed Frank Mike Sheridan.
• Initially it is called as “OAK” and renamed as
“JAVA” in 1995
• Java is an object oriented and multi threaded
programming language.
PRIMARY MOTIVATION
General purpose Language, language used for embedded
systems.
Initially used for Consumer Devices like microwave ovens,
televisions, hand-held remote control named Star 7.
FEATURES OF JAVA
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
• Dynamic
• Simple:
– Java was designed to be easy for the professional
programmer to learn and use effectively.
– If you already understand the basic concepts of object-
oriented programming, learning Java will be even easier.
– In Java, there are a small number of clearly defined ways
to accomplish a given task.
• Secure
– Java does not use memory pointers explicitly. All the
programs in java are run under an area known as the
sand box.
– Security manager determines the accessibility options of
a class like reading and writing a file to the local disk.
– Java uses the public key encryption system to allow the
java applications to transmit over the internet in the
secure encrypted form.
– The bytecode Verifier checks the classes after loading.
• Portable
– The feature Write-once-run-anywhere makes the java
language portable provided that the system must have
interpreter for the JVM.
– Java also have the standard data size irrespective of
operating system or the processor. These features makes
the java as a portable language.
• Object Oriented
– Java supports object oriented approach whose important
feature is reusability of code
• Robust
– Java has the strong memory allocation and automatic
garbage collection mechanism.
– It provides the powerful exception handling and type
checking mechanism as compare to other programming
languages.
– Compiler checks the program whether there any error and
interpreter checks any run time error and makes the system
secure from crash.
• Multithreading
– Java was designed to meet the real-world requirement of creating
interactive, networked programs.
• Architecture Neutral
– The feature “write once; run anywhere, any time, forever.”
makes the java language portable provided that the system
must have interpreter for the JVM.
– Java also have the standard data size irrespective of operating
system or the processor.
• Interpreted
– Java enables the creation of cross-platform programs by
compiling into an intermediate representation called Java
bytecode.
– This code can be interpreted on any system that provides a
Java Virtual Machine.
• Performance
– Java uses native code usage, and lightweight process
called threads. In the beginning interpretation of bytecode
resulted the performance slow but the advance version of JVM
uses the adaptive and just in time compilation technique that
improves the performance.
• Distributed
• Dynamic
– While executing the java program the user can get the
required files dynamically from a local drive or from a
computer thousands of miles away from the user just by
connecting with the Internet.
A Simple Java Program – The Set Up
4.
1. Select Click on
Path OK
2. Click on
Edit 5.
Click
on OK
CLASSPATH
JVM
JAVA
RUN TIME SYSTEM
BYTE
CODE
(.class)
OPERATING SYSTEM
HARDWARE
Java Architecture (Contd.).
Step1:
Create a java source code with .java extension
Step2:
Compile the source code using java compiler, which will
create bytecode file with .class extension
Step3:
Class loader reads both the user defined and library classes
into the memory for execution
Java Architecture (Contd.).
Step4:
Bytecode verifier validates all the bytecodes are valid and
do not violate Java’s security restrictions
Step5:
JVM reads bytecodes and translates into machine code for
execution. While execution of the program the code will
interact to the operating system and hardware
The 5 phases of Java Programs
Java programs can typically be developed in five stages:
1. Edit
Use an editor to type Java program (Welcome.java)
2. Compile
• Usea compiler to translate Java program into an
intermediate language called bytecodes, understood by Java
interpreter (javac Welcome.java)
4. Verify
Use a Bytecode verifier to make sure bytecodes are valid and do not
violate security restrictions
5. Execute
• Java Virtual Machine (JVM) uses a combination of interpretation
and just-in-time compilation to translate bytecodes into machine
language
Variable Names
The variables are in mixed case with a lowercase first letter
Variable names should not start with underscore _ or dollar sign $
characters, even though both are allowed
Variable names should be small yet meaningful
One-character variable names should be avoided except for
temporary “throwaway” variables
Eg: int y,myWidth;
Good Programming Practices(Contd.).
Naming Conventions
Method Names
Methods should be verbs, in mixed case with the first letter lowercase, with the
first letter of each internal word capitalized
Eg: void run(), void getColor()
Comments
Block Comments
(optional)
Block comments are used to provide descriptions of files, methods, data
structures and algorithms
Block comments may be used at the beginning of each file and before each
method
/*
Here is a block comment
*/
Good Programming Practices(Contd.).
Comments
Single line Comment
Single line comments can be written using
// Single line
Number per Line
One declaration per line is recommended
int height;
int width;
is preferred over
int height,width;
Do not put different types on the same line
int height,width[]; // Not recommended
Error Types
Syntax error / Compile errors
– caught at compile time.
– compiler did not understand or compiler does not
allow
Runtime error
– something “Bad” happens at runtime. Java
breaks these into Errors and Exceptions
Logic Error
– program compiles and runs, but does not do
what you intended or want
Operators
class Sample{
public static void main(String[] args){
int a = 10;
int b = 20; Output:
System.out.println("a == b = " + (a == b) );
a == b = false
System.out.println("a != b = " + (a != b) );
a != b = true
System.out.println("a > b = " + (a > b) ); a > b = false
System.out.println("a < b = " + (a < b) ); a < b = true
System.out.println("b >= a = " + (b >= a) ); b >= a = true
System.out.println("b <= a = " + (b <= a) ); b <= a = false
}
}
Logical operator & Unary operator
Operator Name of the Operator Example
&& Logical AND a && b
|| Logical OR a || b
! Logical NOT !(a &&b)
class Sample{
public static void main(String[] args){
boolean a = true;
boolean b = false;
System.out.println("a && b= " + (a&&b));
System.out.println("a || b= " + (a||b));
System.out.println("!(a && b)= "+!a&&b));
}
} Output:
a && b = false
a || b = true
!(a && b) = true
Unary Operator - QUIZ
class Sample{
public static void main(String args[]) { Output:
int a = 10;
int b = 20; ++a = 11
System.out.println(“++a=”+ (++a));
--b = 19
System.out.println(“--b=”+ (--b));
}
}
Quiz-2
What will be the result, if we try to compile and execute the following code?
class Test {
public static void main(String [ ] args) {
int x=10;
int y=5;
System.out.println(++x+(++y));
}
}
OUTPUT: 17
bitwise operator
0 0 1 0 1 1 0 1
~ 0 1 0 0 1 1 1 1 & 0 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1
0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1
^ 0 1 0 0 1 1 1 1 | 0 1 0 0 1 1 1 1
0 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1
Right-Shift and left-shift Operators
• Arithmetic or signed right shift (>>)
operator:
– Examples are:
• 128 >> 1 returns 128/21 = 64
• 256 >> 4 returns 256/24 = 16
• -256 >> 4 returns -256/24 = -16
– The sign bit is copied during the shift.
2. short:
Short data type is a 16-bit signed two's complement integer.
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as byte data type. A
short is 2 times smaller than an int Default value is 0.
Example: short s = 10000, short r = -20000
3. int:
Int data type is a 32-bit signed two's complement integer. Minimum
value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Int is generally used as the default data type for integral values
unless there is a concern about memory. The default value is 0.
Example: int a = 100000, int b = -200000
4. long:
Long data type is a 64-bit signed two's complement integer.
Minimum value is -9,223,372,036,854,775,808.(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
This type is used when a wider range than int is needed.
Default value is 0L.
Example: long a = 100000L, int b = -200000L
5. float:
Float data type is a single-precision 32-bit floating point.
Float is mainly used to save memory in large arrays of floating
point numbers. Default value is 0.0f.
Example: float f1 = 234.5f
6. double:
double data type is a double-precision 64-bit floating point.
This data type is generally used as the default data type for
decimal values, generally the default choice.
Default value is 0.0d.
Example: double d1 = 123.4d
7. boolean:
Boolean data type represents one bit of information. There are
only two possible values: true and false.
This data type is used for simple flags that track true/false
conditions. Default value is false.
Example: boolean one = true
8. char:
char data type is a single Unicode character (instead of ASCII).
Minimum value is '\u0000' (or 0). 16-bit
Maximum value is '\uffff' (or 65,535 inclusive). Char data type is
used to store any character.
Example: char letterA ='A'
Quiz
What will be the result, if we try to compile and execute the following code?
class Test
{
public static void main(String [ ] ar)
{
int for=2;
System.out.println(for);
}
}
Quiz
class Test {
public static void main(String [ ]arg)
{
byte b=128;
System.out.println(b);
}
}
Quiz
class Test {
public static void main(String ar[])
{ float f=1.2;
boolean b=1;
System.out.println(f);
System.out.println(b);
}
}
Quiz(Contd.).
class Test {
public static void
main(String ar[]) {
double d=1.2d;
System.out.println(d);
}
}
OUTPUT: 1.2
Quiz(Contd.).
class Test {
public static void main(String [ ] args)
{
int 9A=10;
System.out.println(9A);
}
}
Types of Variables
The Java programming language defines the following kinds of Variables:
Local Variables
Tied to a method
Tied to an object
Static Variables
Tied to a class