Beruflich Dokumente
Kultur Dokumente
Introduction to JAVA
Bloom’s Taxonomy
1. Knowledge : Exhibits previously learned material by recalling
facts, terms, basic concepts and answers.
(What is ...? How is ...? Where is ...? When did _______ happen? How did ______
happen? How would you explain ...? Why did ...? How would you describe ...?
When did ...? Can you recall ...? How would you show ...? Can you select ...?
Who were the main ...? Can you list three ...? Which one ...? Who was ...?)
1. Comprehension : Demonstrating understanding of facts and
ideas by organizing, comparing, translating, interpreting, giving
descriptions and stating main ideas.
(How would you classify the type of ...? How would you compare ...? contrast ...?
Will you state or interpret in your own words ...? How would you rephrase
the meaning ...? What facts or ideas show ...? What is the main idea of ...?
Which statements support ...? Can you explain what is happening . . . what is
meant . . .? What can you say about ...? Which is the best answer ...? How
would you summarize ...?)
Intel microprocessor
• Java is simple
• Java is object-oriented
• Java is distributed
• Java is interpreted
• Java is robust
• Java is secure
• Java is architecture-neutral
• Java is portable
• Java’s has high performance
• Java is multithreaded
• Java is dynamic
Java Virtual machine (JVM)
• JVM is a component of the java system that interprets and
executes the instructions in our class files.
• It is a software, staying on top of operating system such as UNIX
, WINDOWs etc.,
• It helps java to create high level of portability by hiding the
difference between the Operating System implementations.
– Loading classes
– Object creations of java classes
– Garbage collection
– Security Responsibility
Exploring JVM
Exploring JVM
• The JVM consists of
– Method area
– Heap
– One or more stacks
• Heap The JVM has a heap that is the runtime data area
from which memory for all class instances and arrays
are allocated. It is created at the JVM start-up.
• By default, the maximum heap size is 64 Mb.
• Heap memory for objects is reclaimed by an automatic
memory management system which is known as a
garbage collector. The heap may be of a fixed size or
may be expanded and shrunk, depending on the
garbage collector's strategy.
• The stack is a place in the computer memory where
all the variables that are declared and initialized
before run time are stored.
• [The heap is the section of computer memory where
all the variables created or initialized at run time are
stored ]
Contents of memory Blocks at Run Time
Registers
• The registers of the java virtual machine are just like
the registers inside a real computer.
• FRAME: Pointer to execution environment of current
method.
• VARS: Pointer to the first local variable of current
method.
• STACKS
– JVM is stack based
– The stack is used to supply parameters to byte codes and
methods, and to retrieve results back from them.
– Each stack frame contains three sets of data: the local
variables for the method call, its execution environment, and
its operand stack.
Just In Time Compiler (JIT)
Every time JIT compiler interprets byte code, it will keep the binary
code in log and optimize it. Next time when the same method is
called, the optimized code will run. Sum experiments shows
that, java programs using JIT could be as fast as compiled C
program.
Compiled
Byte JIT Compiler
Optimized Code
& Kept in Log
Native Machine
Code
Machine
Java: Write Once, Run Anywhere
• Consequence of Java’s history:
platform-independence
Windows
compiler
Executable (Windows)
Mac OS
Computer compiler
program
Executable (Mac)
UNIX
compiler
Executable (UNIX)
A High Level View Of Translating/Executing Java Programs
Stage 1: Compilation
Text Editor
Java
Source javadoc HTML Files
Code
Javac
java jdb
Java
Program
output
Introduction: java programming
• Configuration of Java program execution environment
– PATH : Environment variable
• Java program structure
• Program = code + data
• Data types
– Primitive types: Integer type, floating, char, logical
– Non-primitive types : array, string, class and interface
• Code: Programming constructs
– Key words: 50
– Control statements
– Iterative statements
– Operators: Logical, relational and arithmetic
• Coding standards: Pascal and Camel notation
• Reading input to java programs: command Line, Scanner
object
• Array in java
Introduction: java programming : Key words
abstract default implements this
assert do import true
boolean double instanceof throw
break else int throws
byte enum interface while
case extends long return
catch false new short
char final null static
class finally package
const float private
continue for super
Introduction: java programming: Data types
Integer Type Memory in bytes
byte 1
short 2
int 4
long 8
Floating Type
float 4
double 8
Textual
char 2 (Unicode character)
Logical
Boolean 1
Introduction: java programming: Coding standards
• Set of guidelines
– Enhance the readability and clarity of the program
– Make it easy to debug and maintain the program
• Class name: Should follow Pascal Case
i. First letter should be Upper case
ii. First letter of each internal word should be
capitalized (if it is present)
• Instance variable, Reference Variable & method
name(camel notation) i. First letter should be
lower case
ii. . First letter of each internal word should be
capitalized (if it is present)
Introduction: java programming: program structure
[package statement;]
[import statements;]
[Interface definition]
[class definition]
class class_name // starter class
{
public static void main(String[] args)
{
// code
}
}
Introduction: java programming: Example
// starter class or test program
class HelloWorld
{
public static void main(String[] args)
{
System.out.println(“Hello world”);
}
}
Introduction: java programming: Reading input from console
class CmdArgumentsDemo
{
public static void main(String[] args)
{
// prints first element
System.out.println(args[0]);
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
//foreach version in java
/*
for(String s: args)
{
System.out.println(s);
}*/
}
}
Introduction: java programming: Assignment
Write a java program to read two numbers from
user and display sum. Use command line
arguments
Introduction: java programming: Arrays
An array is a collection of similar data in
contiguous locations of memory and referred by
single name.
Arrays can be used to store data belonging to
primitive data types and reference types.
Arrays are created dynamically in java
Types of array
One dimensional array
Multi-Dimensional (2D array)
Introduction: java programming: Creating array
One dimensional
First method
type[] array_name=new type[size];
Example: int[] marks=new int[5];
Initialize:
marks[0]=15; int x= marks[0];
marks[1]=20; // write a java program
marks[2]=16;
marks[3]=17;
marks[4]=20;
marks[5]=18;
Introduction: java programming: Creating array
Array elements will be stored dynamically in the heap
Array elements will be initialized to default values of respective
type if not explicitly initialized
15
20
marks
16
stack 17
20
heap
Introduction: java programming: Creating array
• Two dimensional array
type[][] array_name= new type[size][size];
int [][] LabPcs=new int [5][8];
Initialize:
LabPcs[0][0] = 1;
Labpcs[0][1]=2; // Write a java program
length property: it returns size of array.
Introduction: java programming: Creating array
Second method
example:
String deptNames={“AandR”, “BT”,”CSE”, “ISE” };
Compiler javac
Byte Code
Java interpreter
• String concatenation
• String age=“9”;
• String s=“He is”+age+”years old.”;
• System.out.println(s);
• String concatenation with other datatype
• int age = 9;
• String s=“He is “+age+”years old”;
• System.out.println(s);
• String s=“four:”+2+2;
• System.out.println(s);
• displays
four:22
Rather than
four:4
• String comparision
equals() and equalsIgnoreCase()
boolean equals(Object str)
boolean equalsIgnoreCase(String str)
Class equalsDemo{
public static void main(String args[]){
String s1=“Hello”;
String s2=“Hello”;
String s3=“Good-bye”;
String s4=“HELLO”;
System.out.println(s1+ “equals”+s2+”->”+s1.equals(s2));
System.out.println(s1+ “equals”+s3+”->”+s1.equals(s3));
System.out.println(s1+ “equals”+s4+”->”+s1.equals(s4));
System.out.println(s1+ “equals”+s2+”-
>”+s1.equalsIgnoreCase(s4));
}
}
String
class Testimmutablestring{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method app
//ends the string at the end
System.out.println(s);//will print Sachin because strings are im
mutable objects
}
}
OUTPUT:Sachin
For mutable class, you can use StringBuffer and StringBuilder
class.
String Buffer