0% found this document useful (0 votes)
35 views11 pages

Java Unit 1

This document contains three summaries of technical documents about Java programming: 1. The first document introduces a simple "Hello World" Java program with a main method that prints "Welcome to Java". It discusses compiling and running the Java code. 2. The second document covers Java data types including primitive types like int and double, variables, and type casting. 3. The third document provides an overview of the Java Virtual Machine (JVM) which provides a runtime environment for Java programs. It discusses how Java code is compiled to bytecode, loaded by the JVM, and executed more slowly than other languages.

Uploaded by

Kalyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
35 views11 pages

Java Unit 1

This document contains three summaries of technical documents about Java programming: 1. The first document introduces a simple "Hello World" Java program with a main method that prints "Welcome to Java". It discusses compiling and running the Java code. 2. The second document covers Java data types including primitive types like int and double, variables, and type casting. 3. The third document provides an overview of the Java Virtual Machine (JVM) which provides a runtime environment for Java programs. It discusses how Java code is compiled to bytecode, loaded by the JVM, and executed more slowly than other languages.

Uploaded by

Kalyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

First java program

Class Example
{
Public static void main(String args[])
{
System.out.println(“Welcome to Java”);
}
}
Output:
Welcome to Java

File name – Example.java. (it should be the name of class defined)

Compilation:
C:/> javac Example.java
- To run the program use java application launcher java, pass the class
name Example as command line argument
C:/> java Example

Comment:
Single line comment : // This is example program
Multi line comment: /* This is example program
File name Example.java */
Data Types

- Java is a strongly typed language\


- Every variable has types, assigned values is checked for type
compatibility
Primitive types:
i) Integer
- Byte, short, int and long
ii) Floating point numbers
- Float and double
iii) Characters
- Char (includes symbol in character set like numbers and letters)
iv) Boolean
- True / False

Integers:

Name Width
Long 64
Int 32
Short 16
Byte 8

Floating Point:
Name Width
Double 64
Float 32

Float – specifies single precision


Double – specifies double precision value which is more accurate

Character:
-Size is 16 bit
-uses Unicode to represent characters to achieve global portability
Boolean:
- It has one of two possible values TRUE or FALSE
- Mainly used in relational operators and conditional operators

Variables

Declaring variable:
Type identifier[=value] [,identifier[=value]…];
Ex:
Int a;
Int a=5;
Int a=5,b;
Int a=5,b=5;

Dynamic initialization:
Value of variable can be allocated at runtime
Ex:
Int a = sum(10,20);
Int a = math.sqrt(25);

Scope and life time of variables:


- Local scope is only within the parenthesis
- Ex:
{
Int x=10;
If (x=10)
{
Int y=20;
}
}
X is visible to both inner and outer space but y is visible only to inner
space
OPERATORS
Types of operators
i) Arithmetic operators
ii) Bitwise operators
iii) Relational operators

Arithmetic operators:

+  for addition
- for subtraction
*  for multiplication
/  for dividsion
%  for modulus
++  for increment
+=  addition assignment
-=  subtraction assignment
*=  multiplication assignment
/=  division assignment
%=  modulus assignment\
-- increment

Bitwise operators:

~ unary NOT
&  bitwise AND
|  bitwise OR
^  bitwise ex-OR
>>  Right Shift
>>>  Right shift zero fill
<<  left shift
&=  AND assignment
|=  OR assignement
^=  Ex-OR assignment
>>= , <<=, >>>=  shift assignments
Relational Operators:

==  equal to
!=  does not equal to
> greater than
<  less than
>=  greater than equal to
<=  less than equal to

Method Overloading

- Declare same method in different form


- This is one form of polymorphism in object oriented programming
paradigms
Ex:
- int sum(int a , int b);
- float sum(float a , float b);
- void sum();

Type Casting
- automatic conversion of values from one data type to other
o the two types should be compatible
o destination type should be larger than source type
- for conversion between incompatible type casting must be used
o (target_type) value
int a ,
byte b;
b = (byte) a;
Automatic type promotion:
- When two byte , short values are evaluated the result automatically
converted to short
ARRAYS
- It is a group of like-typed variables that are referred by a common name
- It means grouping related information

One dimensional array:


Type var_name [];
Ex:
Int days[];  it creates NULL value, to assign values use “new” operator

day = new int[12]; // now 12 elements have been created all are assigned to
zero.

To assign values , day[3]=3;

Multi-dimensional array:
- It is arrays of array
- Declaration  int twoD[][]= new int[4][5];

00 01 02 03 04
10 11 12 13 14
20 21 22 2 3 24
30 31 32 33 34

- Left index denotes row, right index denotes column

Alternate syntax:
type [] name;
type [] day1, day2, day3; // creates 3 arrays
CONTROL STATEMENTS
- Type of control statements
i) If
ii) If else if
iii) Switch
iv) Nested switch

If :
Syntax
If (condition)
Statement1;
Else
Statement2;
Ex:
If (a>b)
Print value of a if greater;
Else
Print value of b is greater;
If else If:
Syntax
If (condition)
Statement;
Else if(condition)
Statement;
Else if (condition)
Statement;
-------
-------
Else
Statement;
Switch:

Switch(expression)
{
Case value1:
Statement;
Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;
}

Nested switch:
Switch(expression)
{
Case value1:

Switch(expression)
{
Case value1:
Statement;
Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;

Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;
}

Iteration Statements:
While:
-it repeats the block of statements
While(condition)
{
// body of loop
}

Do-while:
-it first execute the statements and then check for the condition
Do
{
// body of loop;
}while(condition);

For loop:
For(initialization;condition;iteration)
{
//body;
}
For each variation of for:
Syntax
For(type itr-var:collection)
{
//statement block;
}
Itr-var  iteration variable
Collection  from which itr-var get value in cycling manner
Ex:
For (int i=1;i<=10;i++)
{
Sum=sum+i;
}
Same code can be written using for each as follows
Int val[]={1,2,3,4,5,6,7,8,9,10};
For (int x:val)
Sum=sum+i;

JAVA VIRTUAL MACHINE

- It is a engine that provides runtime environment to drive the java code or


application
Software compilation and execution process:
i) editor – to edit the program and save it to disk
ii) compiler- to compile the program to convert it to low level
machine instructions
iii) linker – to link multiple executable files
iv) loader – to load the executable file in to main memory
v) execution – execute the program
- but java uses dynamic linking, class files are linked during execution by
java virtual machine.
- When the java file (for ex. First.java) is compiled , a .class file is created
( first.class) , this class file is a bytecode, class loader component in JVM
loads this byte code into main memory where JVM is running and the
bytecode will be converted into executable code by execution engine. In
this way java combines both compilation and interpretation process that’s
what the execution of java program is slower compared to other
languages like c and c++.
- During execution , JVM running on RAM uses class loader to load all
class files, then using execution engine it converts bytecode into native
machine code.

You might also like