SlideShare a Scribd company logo
JAVA
Fundamental of
java
Prepared by
Miss. Arati A. Gadgil
2
Java
Java was developed in the early 90s by Sun Microsystems
Java is a high-level language.
There are many features of java. They are also known as
java buzzwords.
Simple
Java omits many rarely used, poorly understood, confusing
features of C++. Say : No Pointer! No dynamic delete.
3
Object Oriented
Object –oriented design is a technology that focuses design
on the data (object) and on the interfaces to it.
Everything is an object, everything will become a class in
Java. Every java program, in top- level view, is classes.
Robust
The single biggest difference between Java and C/C++ is
that Java has “a inner safe pointer-model”, therefore it
eliminates the possibility of overwriting memory and
corrupting data, so programmers feel very safe in coding.
4
Distributed
Basically, Java is for Net-Work application, for WEB
project. Java can open and access “objects” across the Net via
URLs (Uniform Resource Locator)
eg. “http//:gamut.neiu.edu/~ylei/home.html”, with the same
ease as when accessing a local file system
Platform Independent
Java code can be run on multiple platforms e.g. Windows,
Linux, Sun Solaris,Mac/OS etc. Java code is compiled by the
compiler and converted into bytecode.This bytecode is a
platform independent code because it can be run on multiple
platforms i.e. Write Once and Run Anywhere(WORA).
5
Secured
Java is secured because:
No explicit pointer,Programs run inside virtual machine
sandbox also,
•Classloader- adds security by separating the package for the
classes of the local file system from those that are imported
from network sources.
•Bytecode Verifier- checks the code fragments for illegal code
that can violate access right to objects.
•Security Manager- determines what resources a class can
access such as reading and writing to the local disk
6
Architecture-neutral
The compiler generates an architecture-neutral object file
format. The compile code can run on many processors ,given
the presence of the runtime system. The java compiler does
this by generting bytecode instruction which have nothing to
do with a particular computer architecture.
Portable
We may carry the java bytecode to any platform.
High-performance
Java is faster than traditional interpretation since byte code is
"close" to native code still somewhat slower than a compiled
language (e.g., C++)
7
Multi-threaded
A thread is like a separate program, executing concurrently.
We can write Java programs that deal with many tasks at once
by defining multiple threads. The main advantage of multi-
threading is that it shares the same memory. Threads are
important for multi-media, Web applications etc.
Dynamic
In number of ways, java is more dynamic language than C or
C++.It was designed to adapt to an evolving environment.
Libraries can freely add new methods and instance variables
without any effect on their clients. In java finding out runtime
type information is straightforward.
8
Java Virtual Machine
The .class files generated by the compiler are not executable
binaries, Java combines compilation and interpretation.
Instead, they contain “byte-codes” to be executed by the Java
Virtual Machine.
This approach provides platform independence, and greater
security.
The JVM performs following main tasks:
•Loads code
•Verifies code
•Executes code
•Provides runtime environment
9
JRE
JRE is an acronym for Java Runtime Environment. It is used
to provide runtime environment.It is the implementation of
JVM. It physically exists.It contains set of libraries + other
files that JVM uses at runtime.
JDK
JDK is an acronym for Java Development Kit. It physically
exists. It contains JRE + development tools
10
JVM
Java Virtual Machine (JVM) is an abstract computing
machine. Java Runtime Environment (JRE) is an
implementation of the JVM. Java Development Kit (JDK)
contains JRE along with various development tools like Java
libraries, Java source compilers, Java debuggers, bundling and
deployment tools.
11
Relations between JVM JRE JDK
12
JIT
JIT refers to execution engine in few of JVM
implementations, one that is faster but requires more memory,
is a just-in-time compiler. In this scheme, the bytecodes of a
method are compiled to native machine code the first time the
method is invoked.
JIT refers to execution engine in few of JVM
implementations, one that is faster but requires more
memory,is a just-in-time compiler. In this scheme, the
bytecodes of a method are compiled to native machine code
the first time the method is invoked.
13
Java Bytecode
Java bytecode is the instruction set of the Java virtual
machine. Eachbytecode is composed by one, or in some cases
two, bytes that represent the instruction (opcode), along with
zero or more bytes for passing parameters..
Bytecode is nothing but the intermediate representation of
Java source code which is produced by the Java compiler by
compiling that source code. This byte code is an machine
independent code.It is not an completely a compiled code but
it is an intermediate code somewhere in the middle which is
later interpreted and executed by JVM
14
HotSpot, released as the "Java HotSpot Performance Engine"is
a Java virtual machine for desktops and servers, maintained and
distributed by Oracle Corporation. It features techniques such as just-
in-time compilation and adaptive optimization designed to improve
performance.
Sun intended to write a new just-in-time (JIT) compiler for the
newly developed virtual machine.This new compiler would give rise
to the name "HotSpot", which derives from the fact that, as the
software runs Java bytecode, it continually analyzes the program's
performance for "hot spots" which are frequently or repeatedly
executed. These are then targeted for optimization, leading to high-
performance execution with a minimum of overhead for less
performance-critical code
Java HotSpot
15
Sun's JRE features two virtual machines, one called Client and the
other Server. The Client version is tuned for quick loading. It makes use
of interpretation. The Server version loads more slowly, putting more
effort into producing highly optimized JI compilations, that yield higher
performance. Both VMs compile only often-run methods, using a
configurable invocation-count threshold to decide which methods to
compile.
The HotSpot Java virtual machine is written in C++. As stated on the
HotSpot web page, the source contains approximately 250,000 lines of
code.
Hotspot provides:
•A class loader
•A bytecode interpreter
•Client and Server virtual machines, optimized for their respective uses
•Several garbage collectors
•A set of supporting runtime libraries
16
The main() method
public static void main(String args[])
{
...
}
public--- the interpreter can call it.
static ----It is a static method belonging to the class.
void -----It does not return a value.
String----It always has an array of String objects as its formal parameter.
the array contains any arguments passed to the program on the
command line.The source file’s name must match the class name which
main method is in.
17
Source File Declaration Rules
There can be only one public class per source code file.
Comments can appear at the beginning or end of any line in the source
code file; they are independent of any of the positioning rules discussed
here.
If there is a public class in a file, the name of the file must match the
name of the public class.
For example, a class declared as public class Dog { }must be in a source
code file named Dog.java.
If the class is part of a package, the package statement must be the first
line in the source code file, before any import statements that may be
present.
18
If there are import statements, they must go between the package
statement (if there is one) and the class declaration. If there isn't a
package statement, then the import statement(s) must be the first line(s)
in the source code file.
If there are no package or import statements, the class declaration must
be the first line in the source code file.
import and package statements apply to all classes within a source
code file. In other words, there's no way to declare multiple classes in a
file and have them in different packages, or use different imports.
A file can have more than one nonpublic class.
Files with no public classes can have a name that does not match any of
the classes in the file
19
Comments
/* This kind of comment can span multiple lines */
// This kind is to the end of the line
/**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
20
Primitive types
•int 4 bytes
•short 2 bytes
•long 8 bytes
•byte 1 byte
•float 4 bytes
•double 8 bytes
•char Unicode encoding (2 bytes)
•boolean {true,false}
Behaviors is
exactly as in
C++
21
• Constants
37 integer
37.2 float
42F float
0754 integer (octal)
0xfe integer (hexadecimal)
• Comparison operators
== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
22
Variable declaration
type variable-name;
Meaning: variable <variable-name> will be a variable of type <type>
Where type can be:
int //integer
double //real number
Example:
int a, b, c;
double x;
int sum;
23
String is an Object
Constant strings as in C, does not exist
 The function call show(“Hello”) creates a String object, containing
“Hello”, and passes reference to it to show.
 The String object is a constant. It can’t be changed using a reference
to
it.
24
Writing to user (output)
System.out.println(variable-name);
prints the value of variable <variable-name> to the user
System.out.println(“any message “);
prints the message within quotes to the user
System.out.println(“hello” + “world” + a + “plus“ + b);
assuming the value of a is 3 and of b is 7, it prints
helloworld3plus7
Note: System.out.println() always prints on a new line.
25
Example
/*
This program illustrates the System.out.println command.
*/
public class Hello
{
public static void main (String args[])
{
System.out.println(“This is my first Java program!”);
System.out.print(“I like Java.”);
System.out.print(“I think Java is cool.”);
} // end of main
} // end of class
26
Example
/*
Printing ages.
*/
public class MyFirstJavaProgram
{
public static void main (String args[])
{
int myAge, myFriendAge; /* declare two integer
variables */
myAge = 20;
myFriendAge = myAge + 1; //one year older
System.out.println(“Hello, I am “ +myAge + “years old,
and my friend is “ + myFriendAge + “ years old”);
System.out.println(“Goodbye”);
} // end of main
} // end of class
27
Flow control
if/else
do/while
for
switch
If(x==4) {
// act1
} else {
// act2
}
int i=5;
do {
// act1
i--;
} while(i!=0);
int j;
for(int i=0;i<=9;i++)
{
j+=i;
}
char
c=IN.getChar();
switch(c) {
case ‘a’:
case ‘b’:
// act1
break;
default:
// act2
}
28
Big Numbers
If the precision of the basic integer and floating-point types is not
sufficient, you can turn to a couple of handy classes in
the java.math package, called BigInteger and BigDecimal. These are
classes for manipulating numbers with an arbitrarily long sequence of
digits. The BigInteger class implements arbitrary precision integer
arithmetic, and BigDecimal does the same for floating-point numbers.
Use the static valueOf method to turn an ordinary number into a big
number:
BigInteger a = BigInteger.valueOf(100);
BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));
// d = c * (b + 2)
29
Array
•Array is an object
• Array size is fixed
A[] arr; // nothing yet …
arr = new A[4]; // only array of pointers
for(int i=0 ; i < arr.length ; i++)
arr[i] = new Animal();
30
Arrays - Multidimensional
• In C++
Animal arr[2][2]
Is:
• In Java
What is the type of
the object here ?
A[][] arr=
new A[2][2]
Thank You
31

More Related Content

PDF
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
PDF
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
PPTX
Core Java Tutorials by Mahika Tutorials
PPTX
Core java
PDF
Basic Java Programming
PDF
Introduction to java (revised)
PPTX
Introduction to java
PPTX
Core Java
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Core Java Tutorials by Mahika Tutorials
Core java
Basic Java Programming
Introduction to java (revised)
Introduction to java
Core Java

What's hot (20)

PPTX
History Of JAVA
PDF
Introduction to java technology
PPTX
Advance Java Topics (J2EE)
PPT
Spring Boot in Action
PPT
Java EE Introduction
PDF
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
PPTX
JAVA INTRODUCTION - 1
PDF
Java Presentation For Syntax
PPT
Java Networking
PPTX
Introduction to java
PPSX
Introduction to java
PPTX
JAVA-PPT'S.pptx
PDF
Introduction to Java Programming Language
PPSX
Java &amp; advanced java
PPTX
Java - Generic programming
PPTX
Lecture - 1 introduction to java
PDF
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
PPSX
Arrays in Java
PPTX
Introduction to java
PDF
Java Basic Oops Concept
History Of JAVA
Introduction to java technology
Advance Java Topics (J2EE)
Spring Boot in Action
Java EE Introduction
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
JAVA INTRODUCTION - 1
Java Presentation For Syntax
Java Networking
Introduction to java
Introduction to java
JAVA-PPT'S.pptx
Introduction to Java Programming Language
Java &amp; advanced java
Java - Generic programming
Lecture - 1 introduction to java
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Arrays in Java
Introduction to java
Java Basic Oops Concept
Ad

Viewers also liked (17)

PPT
Chapter 4 Powerpoint
PPT
C0 review core java1
ZIP
Introduction to the Java(TM) Advanced Imaging API
PDF
Java Programming - 04 object oriented in java
PPT
Eo gaddis java_chapter_02_5e
PDF
Java Programming - 03 java control flow
PPTX
Intro to C++ Basic
PDF
Java for beginners
PPT
Core Java Basics
PDF
Java Programming - 01 intro to java
PPTX
Basics of Java
PDF
02 basic java programming and operators
PPT
Java Basics
PPT
Core java concepts
PPTX
Java programming course for beginners
PDF
Introduction to Java Programming
PPT
Java tutorial PPT
Chapter 4 Powerpoint
C0 review core java1
Introduction to the Java(TM) Advanced Imaging API
Java Programming - 04 object oriented in java
Eo gaddis java_chapter_02_5e
Java Programming - 03 java control flow
Intro to C++ Basic
Java for beginners
Core Java Basics
Java Programming - 01 intro to java
Basics of Java
02 basic java programming and operators
Java Basics
Core java concepts
Java programming course for beginners
Introduction to Java Programming
Java tutorial PPT
Ad

Similar to Java basic (20)

PPTX
Skillwise Elementary Java Programming
PPTX
UNIT 1.pptx
PPTX
PPTX
2 22CA026_Advance Java Programming_Data types and Operators.pptx
PPTX
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
PPTX
oop unit1.pptx
PPTX
Object Oriented Programming Part 1 of Unit 1
PDF
OOPS JAVA.pdf
PDF
Core java part1
PPT
Introduction to Java Programming, Basic Structure, variables Data type, input...
PPT
Java-Unit-I.ppt
PPT
Object Oriented Programming-JAVA
DOCX
Srgoc java
PPTX
Java session2
PDF
Lec 3 01_aug13
PPTX
Java fundamentals
PPTX
Java lab zero lecture
PDF
Java programming material for beginners by Nithin, VVCE, Mysuru
Skillwise Elementary Java Programming
UNIT 1.pptx
2 22CA026_Advance Java Programming_Data types and Operators.pptx
JAVA PROGRAM CONSTRUCTS OR LANGUAGE BASICS.pptx
oop unit1.pptx
Object Oriented Programming Part 1 of Unit 1
OOPS JAVA.pdf
Core java part1
Introduction to Java Programming, Basic Structure, variables Data type, input...
Java-Unit-I.ppt
Object Oriented Programming-JAVA
Srgoc java
Java session2
Lec 3 01_aug13
Java fundamentals
Java lab zero lecture
Java programming material for beginners by Nithin, VVCE, Mysuru

More from Arati Gadgil (16)

PPT
Java adapter
PPT
Java swing
PPT
Java applet
PPT
Java layoutmanager
PPT
Java awt
PPT
Java stream
PPT
Java thread
PPT
Java networking
PPT
Java jdbc
PPT
Java package
PPT
Java interface
PPT
Java inheritance
PPT
Java eventhandling
PPT
Java exception
PPT
Java collection
PPT
Java class
Java adapter
Java swing
Java applet
Java layoutmanager
Java awt
Java stream
Java thread
Java networking
Java jdbc
Java package
Java interface
Java inheritance
Java eventhandling
Java exception
Java collection
Java class

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Business Ethics Teaching Materials for college
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
TR - Agricultural Crops Production NC III.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Types and Its function , kingdom of life
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pre independence Education in Inndia.pdf
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
human mycosis Human fungal infections are called human mycosis..pptx
Business Ethics Teaching Materials for college
102 student loan defaulters named and shamed – Is someone you know on the list?
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
RMMM.pdf make it easy to upload and study

Java basic

  • 2. 2 Java Java was developed in the early 90s by Sun Microsystems Java is a high-level language. There are many features of java. They are also known as java buzzwords. Simple Java omits many rarely used, poorly understood, confusing features of C++. Say : No Pointer! No dynamic delete.
  • 3. 3 Object Oriented Object –oriented design is a technology that focuses design on the data (object) and on the interfaces to it. Everything is an object, everything will become a class in Java. Every java program, in top- level view, is classes. Robust The single biggest difference between Java and C/C++ is that Java has “a inner safe pointer-model”, therefore it eliminates the possibility of overwriting memory and corrupting data, so programmers feel very safe in coding.
  • 4. 4 Distributed Basically, Java is for Net-Work application, for WEB project. Java can open and access “objects” across the Net via URLs (Uniform Resource Locator) eg. “http//:gamut.neiu.edu/~ylei/home.html”, with the same ease as when accessing a local file system Platform Independent Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris,Mac/OS etc. Java code is compiled by the compiler and converted into bytecode.This bytecode is a platform independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere(WORA).
  • 5. 5 Secured Java is secured because: No explicit pointer,Programs run inside virtual machine sandbox also, •Classloader- adds security by separating the package for the classes of the local file system from those that are imported from network sources. •Bytecode Verifier- checks the code fragments for illegal code that can violate access right to objects. •Security Manager- determines what resources a class can access such as reading and writing to the local disk
  • 6. 6 Architecture-neutral The compiler generates an architecture-neutral object file format. The compile code can run on many processors ,given the presence of the runtime system. The java compiler does this by generting bytecode instruction which have nothing to do with a particular computer architecture. Portable We may carry the java bytecode to any platform. High-performance Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g., C++)
  • 7. 7 Multi-threaded A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi- threading is that it shares the same memory. Threads are important for multi-media, Web applications etc. Dynamic In number of ways, java is more dynamic language than C or C++.It was designed to adapt to an evolving environment. Libraries can freely add new methods and instance variables without any effect on their clients. In java finding out runtime type information is straightforward.
  • 8. 8 Java Virtual Machine The .class files generated by the compiler are not executable binaries, Java combines compilation and interpretation. Instead, they contain “byte-codes” to be executed by the Java Virtual Machine. This approach provides platform independence, and greater security. The JVM performs following main tasks: •Loads code •Verifies code •Executes code •Provides runtime environment
  • 9. 9 JRE JRE is an acronym for Java Runtime Environment. It is used to provide runtime environment.It is the implementation of JVM. It physically exists.It contains set of libraries + other files that JVM uses at runtime. JDK JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools
  • 10. 10 JVM Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.
  • 12. 12 JIT JIT refers to execution engine in few of JVM implementations, one that is faster but requires more memory, is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked. JIT refers to execution engine in few of JVM implementations, one that is faster but requires more memory,is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked.
  • 13. 13 Java Bytecode Java bytecode is the instruction set of the Java virtual machine. Eachbytecode is composed by one, or in some cases two, bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters.. Bytecode is nothing but the intermediate representation of Java source code which is produced by the Java compiler by compiling that source code. This byte code is an machine independent code.It is not an completely a compiled code but it is an intermediate code somewhere in the middle which is later interpreted and executed by JVM
  • 14. 14 HotSpot, released as the "Java HotSpot Performance Engine"is a Java virtual machine for desktops and servers, maintained and distributed by Oracle Corporation. It features techniques such as just- in-time compilation and adaptive optimization designed to improve performance. Sun intended to write a new just-in-time (JIT) compiler for the newly developed virtual machine.This new compiler would give rise to the name "HotSpot", which derives from the fact that, as the software runs Java bytecode, it continually analyzes the program's performance for "hot spots" which are frequently or repeatedly executed. These are then targeted for optimization, leading to high- performance execution with a minimum of overhead for less performance-critical code Java HotSpot
  • 15. 15 Sun's JRE features two virtual machines, one called Client and the other Server. The Client version is tuned for quick loading. It makes use of interpretation. The Server version loads more slowly, putting more effort into producing highly optimized JI compilations, that yield higher performance. Both VMs compile only often-run methods, using a configurable invocation-count threshold to decide which methods to compile. The HotSpot Java virtual machine is written in C++. As stated on the HotSpot web page, the source contains approximately 250,000 lines of code. Hotspot provides: •A class loader •A bytecode interpreter •Client and Server virtual machines, optimized for their respective uses •Several garbage collectors •A set of supporting runtime libraries
  • 16. 16 The main() method public static void main(String args[]) { ... } public--- the interpreter can call it. static ----It is a static method belonging to the class. void -----It does not return a value. String----It always has an array of String objects as its formal parameter. the array contains any arguments passed to the program on the command line.The source file’s name must match the class name which main method is in.
  • 17. 17 Source File Declaration Rules There can be only one public class per source code file. Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here. If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { }must be in a source code file named Dog.java. If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  • 18. 18 If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file. import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports. A file can have more than one nonpublic class. Files with no public classes can have a name that does not match any of the classes in the file
  • 19. 19 Comments /* This kind of comment can span multiple lines */ // This kind is to the end of the line /** * This kind of comment is a special * ‘javadoc’ style comment */
  • 20. 20 Primitive types •int 4 bytes •short 2 bytes •long 8 bytes •byte 1 byte •float 4 bytes •double 8 bytes •char Unicode encoding (2 bytes) •boolean {true,false} Behaviors is exactly as in C++
  • 21. 21 • Constants 37 integer 37.2 float 42F float 0754 integer (octal) 0xfe integer (hexadecimal) • Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal
  • 22. 22 Variable declaration type variable-name; Meaning: variable <variable-name> will be a variable of type <type> Where type can be: int //integer double //real number Example: int a, b, c; double x; int sum;
  • 23. 23 String is an Object Constant strings as in C, does not exist  The function call show(“Hello”) creates a String object, containing “Hello”, and passes reference to it to show.  The String object is a constant. It can’t be changed using a reference to it.
  • 24. 24 Writing to user (output) System.out.println(variable-name); prints the value of variable <variable-name> to the user System.out.println(“any message “); prints the message within quotes to the user System.out.println(“hello” + “world” + a + “plus“ + b); assuming the value of a is 3 and of b is 7, it prints helloworld3plus7 Note: System.out.println() always prints on a new line.
  • 25. 25 Example /* This program illustrates the System.out.println command. */ public class Hello { public static void main (String args[]) { System.out.println(“This is my first Java program!”); System.out.print(“I like Java.”); System.out.print(“I think Java is cool.”); } // end of main } // end of class
  • 26. 26 Example /* Printing ages. */ public class MyFirstJavaProgram { public static void main (String args[]) { int myAge, myFriendAge; /* declare two integer variables */ myAge = 20; myFriendAge = myAge + 1; //one year older System.out.println(“Hello, I am “ +myAge + “years old, and my friend is “ + myFriendAge + “ years old”); System.out.println(“Goodbye”); } // end of main } // end of class
  • 27. 27 Flow control if/else do/while for switch If(x==4) { // act1 } else { // act2 } int i=5; do { // act1 i--; } while(i!=0); int j; for(int i=0;i<=9;i++) { j+=i; } char c=IN.getChar(); switch(c) { case ‘a’: case ‘b’: // act1 break; default: // act2 }
  • 28. 28 Big Numbers If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package, called BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers. Use the static valueOf method to turn an ordinary number into a big number: BigInteger a = BigInteger.valueOf(100); BigInteger c = a.add(b); // c = a + b BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
  • 29. 29 Array •Array is an object • Array size is fixed A[] arr; // nothing yet … arr = new A[4]; // only array of pointers for(int i=0 ; i < arr.length ; i++) arr[i] = new Animal();
  • 30. 30 Arrays - Multidimensional • In C++ Animal arr[2][2] Is: • In Java What is the type of the object here ? A[][] arr= new A[2][2]