Java Unit 1.1
Java Unit 1.1
What is Java ?
Java is a “Programming Language”.
What is a programming Language?
Spoken Programming
Languages Languages
1.Simple
(easy to learn and write)
2.Secure
(provides data security through encapsulation )
3.Portable
(they can be executed on any kind OS)
4.Object-Oriented
(it is complete object oriented programming)
5.Robust
(it is very strong with type checking
6.Multithreaded
( java support Multithreading)
7.Architecture neutral
(java is platform independent)
8.Interpreted
(java maintain both compiler and interpreter)
9.High Performance
(java maintain JIT compiler)
10.Distributed
(Java supports distributed computation using Remote
Method Invocation (RMI) concept. )
11.Dynamic
(Libraries are dynamically linked during runtime
Java Versions
Class :
-> A Class contain collection of member variables and
member methods.
-> We can create more than one object to a single class.
-> Through the object only we can access the class
members.
-> Class provide security to all its data members.
-> The java best feature encapsulation implemented by
classes .
public static void main(String args[]):
Public : it means that method can be used by code outside
of its class
static : it is used when we want to access a method without
creating its object, as we call the main method
before creating any class objects.
void : it indicates that a method does not return a value.
main() is declared as void because it does not return a
value.
main : it is a method; this is a starting point of a Java program.
String args[]: it is the input parameter for main method
System.out.println():
System is the predefine class ,out is the object and println is
the method.
Sample program:
C program to print “Hello Java program to print “Hello
World !”: World!”:
Protected
#include <stdio.h> import java.io.*; by class
class Simple
{
void main() public static void main( String
{ args[])
printf("Hello World!"); {
} System.out.println("Hello Java");
}
}
}
}
Data Types, Variables and Arrays
Data Types :
Data Type Size Description
Where type is one of Java's types (such as int or String), and variable is
the name of the variable (such as x or name). The equal sign is used to
assign values to the variable.
To create a variable that should store text, look at the following Example
Create a variable called name of type String and assign it the value
"John":
String name = "John";
System.out.println(name);
VARIABLE TYPES
A class can contain any of the following variable types.
Local variables − Variables defined inside methods,
constructors or blocks are called local variables. The variable
will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
Instance variables − Instance variables are variables within a
class but outside any method. These variables are initialized
when the class is instantiated. Instance variables can be
accessed from inside any method, constructor or blocks of that
particular class.
Class variables − Class variables are variables declared within
a class, outside any method, with the static keyword(can be
used to refer the common property of all objects.)
Arrays
Declaring an Array Variable:
Do not have to create an array while declaring array
variable
<type> [] variable_name;
<type> variable_name[];
Ex:
int [] prime;
int prime[];
Both syntaxes are equivalent
No memory allocation at this point
Defining/Creation an Array
Define an array as follows:
variable_name=new <type>[N];
Ex: primes=new int[10];
prime Index
0 1 2 3 4 5 6 7 8 9
2 1 11 -9 2 1 11 90 101 2
value
What happens if …
We define
int[] prime=new long[20];
MorePrimes.java:5: incompatible types
found: long[]
required: int[]
int[] primes = new long[20];
^
The right hand side defines an array, and thus the
array variable should refer to the same type of
array
Default Initialization
When array is created, array elements are
initialized default as
Numeric values (int, double, etc.) to 0
Boolean values to false
Char values to ‘\u0000’ (unicode for
blank character)
Class types to null
Initializing Arrays
Initialize and specify size of array while declaring
an array variable
int[] primes={2,3,5,7,11,13,17}; //7 elements
You can initialize array with an existing array
int[] even={2,4,6,8,10};
int[] value=even;
One array but two array variables!
Both array variables refer to the same array
Array can be accessed through either variable name
Sample Program
class MinAlgorithm
{
public static void main ( String[] args )
{
int[] array = { -20, 19, 1, 5, -1, 27, 19,
5 } ;
int min=array[0]; // initialize the current
minimum
for ( int i=0; i < array.length; i++ )
if ( array[ i ] < min )
min = array[ i] ;
System.out.println("The minimum of this array is: " +
min );
}
}
O/P: The minimum of this array is: -20
Arrays of Arrays
Two-Dimensional arrays
float[][] temperature=new float[10][365];
10 arrays each having 365 elements
First index: specifies array (row)
Second Index: specifies element in that array
(column)
In JAVA float is 4 bytes, total Size=4*10*365=14,600
bytes
EXP : int[][] array2D = { {99, 42, 74, 83,
100} , {90, 91, 72, 88, 95}, {88, 61, 74, 89,
96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99},
{50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} };
//5 arrays with 5 elements each
Sample Program
class unevenExample3
{
public static void main( String[] arg )
{ // declare and construct a 2D array
int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0,
1, 2, 3, 4 } };
// print out the array
for ( int row=0; row < uneven.length; row++ )
//changes row
{
System.out.print("Row " + row + ": ");
for ( int col=0; col <
uneven[row].length; col++ ) //changes column
Output :
System.out.print( uneven[row][col]
Row 0: 1 9 4
+ " "); System.out.println();
Row 1: 0
} 2
} Row 2: 0
TYPE CONVERSION AND CASTING
Java provides various datatypes to store various data values.
Converting one primitive datatype into another is known as type casting (type
conversion) in Java.
Widening − Converting a lower datatype to a higher datatype is known as widening. In
this case the casting/conversion is done automatically by the comiler therefore, it is
known as implicit type casting. In this case both datatypes should be compatible with
each other.