Programming in Java
Programming in Java
(BCAC0012)
Introduction to Java
• Java is a programming language and a platform.
• Embedded System
• Smart Card
Cont…
• Robotics
• Games, etc.
Types of Java Applications
• There are mainly 4 types of applications that can be created
using Java programming:
1) Standalone Application
• Standalone applications are also known as desktop
applications or window-based applications.
Cont…
• These are traditional software that we need to install on every
machine.
2. Object-Oriented
3. Portable
4. Platform independent
Cont…
5. Secured
6. Robust
7. Architecture neutral
1. Simple
• According to Sun, Java language is a simple programming
language because:
A. Class
B. Object
C. Inheritance
Cont…
D. Polymorphism
E. Abstraction
F. Encapsulation
Cont…
A. Class
• A class is a group of objects which have common properties.
• Methods
• Constructors
• Blocks
D. Polymorphism
• Polymorphism in Java is a concept by which we can perform
a single action in different ways.
Cont…
• Polymorphism is derived from 2 Greek words: poly and
morphs.
Call by Value and Call by C++ supports both call by value Java supports call by value only.
reference and call by reference. There is no call by reference in
java.
Structure and Union C++ supports structures and Java doesn't support structures
unions. and unions.
Java Bytecode
• Java code is compiled by the compiler and converted into
bytecode.
• Verifies code
• Executes code
4) Stack
• It holds local variables.
Cont…
5) Program Counter Register
• PC (program counter) register contains the address of the Java
virtual machine instruction currently being executed.
• It physically exists.
• It contains a set of libraries + other files that JVM uses at runtime.
JDK
06. case 07. catch 08. char 09. continue 10. default
21. import 22. instanceof 23. int 24. interface 25. long
26. native 27. new 28. package 29. private 30. protected
31. public 32. return 33. short 34. static 35. super
36. switch 37. synchronized 38. this 39. throw 40. throws
41. transient 42. try 43. void 44. volatile 45. while
46. assert 47. const 48. enum 49. goto 50. strictfp
Cont…
• The keywords const and goto are reserved but not used.
Identifiers
• Identifiers are used to name a variable, constant, function,
class, and array.
• high-temp
• not/ok
Literals
• In programming literal is a notation that represents a fixed
value (constant) in the source code.
• Documentation Comment
1) Java Single Line Comment
• The single line comment is used to comment only one line.
• Syntax:
//This is single line comment
Example:
class CommentExample1
{
public static void main(String a[])
{
int i=10; //Here, i is a variable
System.out.println(i);
}
}
Output: 10
2) Java Multi Line Comment
• The multi line comment is used to comment multiple lines of
code.
• Syntax:
/*
This
is
multi line
comment
*/
Example:
class CommentExample2
{
public static void main(String a[])
{
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
}
}
Output: 10
3) Java Documentation Comment
• The documentation comment is used to create documentation
API.
• Syntax:
/**
This
is
documentation
comment
*/
Example:
/** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/
• Now, there will be HTML files created for your Calculator class in the
current directory.
Whitespace
• Java is a free-form language.
• Data types specify the different sizes and values that can be
stored in the variable.
Cont…
• There are two types of data types in Java:
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
• instance variable
• static variable
1) Local Variable
• A variable declared inside the body of the method, block and
constructor is called local variable.
• You can use local variable only within that method, block and
constructor in which it is declared.
Cont…
• A local variable cannot be defined with "static" keyword.
2) Instance Variable
• A variable declared inside the class but outside the body of
the method, block and constructor is called instance variable.
• It is done automatically.
Cont…
• It takes place when:
• Both data types must be compatible with each other.
• byte -> short -> char -> int -> long -> float -> double
Example
class A
{
public static void main(String a[])
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
Output
7
7
7.0
Narrowing Type Casting
• Converting a higher data type into a lower one is
called narrowing type casting.
• double -> float -> long -> int -> char -> short -> byte
Example
class A
{
public static void main(String a[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println(d);
//fractional part lost
System.out.println(l);
//fractional part lost
System.out.println(i);
}
}
Output
166.66
166
166
Java Command Line Arguments
• The java command-line argument is an argument i.e. passed
at the time of running the java program.
class A
{
public static void main(String a[])
{
System.out.println(a[0]);
}
}
Compile
javac A.java
run
java A 10
Output
10
Java Arrays
• Normally, an array is a collection of similar type of elements
which has contiguous memory location.
• Multidimensional Array
Single Dimensional (1-D) Array in Java
• An Array that will contain only one subscript is called Single
Dimensional Array.
Syntax to Declare 1-D Array in Java
• dataType arrayname[]; (or)
• dataType []arrayname;
Example
• int a[];
Instantiation of 1-D Array in Java
• Syntax:
• arrayname =new datatype[size];
• Example:
• a=new int[3];
Declaration and instantiation in one line
• Example:
• int a[] = new int[3];
Example of Java 1-D Array
class Testarray
{
public static void main(String a1[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0; i<a.length; i++) //length is the property of array
System.out.println(a[i]);
}
}
Declaration, Instantiation and
Initialization of Java 1-D Array
• We can declare, instantiate and initialize the java array together.
• Example:
• int a[]={33,3,4,5};//declaration, instantiation and initialization
Example
class Testarray1
{
public static void main(String a[])
{
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}
}
For-each Loop for Java Array
• The Java for-each loop prints the array elements one by one.
The syntax of the for-each loop is given below:
for(datatype variablename:arrayname)
{
//body of the loop
}
Example
//Java Program to print the array elements using for-each loop
class Testarray1
{
public static void main(String a[])
{
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i : arr)
System.out.println(i);
}
}
Multidimensional Array in Java
• An Array that will contain more than one subscript is called Multi
Dimensional Array.
Types of Multidimensional Array
• 2-D Array
• 3-D Array
• n-D Array
2-D Array
• An Array that will contain two subscript is called Two Dimensional
Array.
Syntax to Declare 2-D Array in Java
• dataType[][] arrayname; (or)
• dataType arrayname[][];
• Example
• int a[][] ;
Instantiation of 2-D Array in Java
• Syntax:
• arrayname =new datatype[rowsize][columnsize];
• Example:
• a=new int[3][3];
•
Declaration and instantiation in one line
• Example:
• int a[][] = new int[3][3];
Example to initialize 2-D Array in Java
• arr[0][0]=1;
• arr[0][1]=2;
• arr[0][2]=3;
• arr[1][0]=4;
• arr[1][1]=5;
• arr[1][2]=6;
• arr[2][0]=7;
• arr[2][1]=8;
• arr[2][2]=9;
Declaration, Instantiation and
Initialization of Java 2-D Array
• We can declare, instantiate and initialize the java array together.
• Example:
• int a[][]={{1,2,3},{4,5,6}};//declaration, instantiation and initialization
Example of 2-D Array
class A
{
public static void main(String a[])
{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}
Java String
• Generally, String is a sequence of characters.
• By new keyword
1) String Literal
• Java String literal is created by using double quotes.
• Example:
• String s="welcome";
Cont…
• Each time you create a string literal, the JVM checks the "string
constant pool" first.
• If the string doesn't exist in the pool, a new string instance is created
and placed in the pool.
Cont…
• Example:
• String s1="Welcome";
• In such case, JVM will create a new string object in normal (non-pool)
heap memory, and the literal "Welcome" will be placed in the string
constant pool. The variable s will refer to the object in a heap (non-
pool).
Immutable String in Java
• In java, string objects are immutable. Immutable simply
means unmodifiable or unchangeable.
Output:Sachin
Why string objects are immutable in java?
Output:
javatpoint hello string
Cont…
• String toUpperCase(): returns a string in uppercase.
Example
public class StringUpperExample
{
public static void main(String a[])
{
String s1="hello string";
System.out.println(s1.toUpperCase());
}
}
Output:
HELLO STRING
String Concatenation in Java
• There are two ways to concat string in java:
• By + (string concatenation) operator
• By concat() method
1) String Concatenation by +
(string concatenation) operator
• Java string concatenation operator (+) is used to add strings.
Example 1
class TestStringConcatenation1
{
public static void main(String a[])
{
String s="Sachin"+"Tendulkar";
System.out.println(s);
}
}
Output: SachinTendulkar
Example 2
class TestStringConcatenation2
{
public static void main(String a[])
{
String s=50+30+"Sachin"+40+40;
System.out.println(s);
}
}
Output: 80Sachin4040
Cont…
• Note: After a string literal, all the + will be treated as string
concatenation operator.
2) String Concatenation by concat() method
• The String concat() method concatenates the specified string to the end
of current string.
• Syntax:
• public String concat(String another)
Example
class TestStringConcatenation3
{
public static void main(String a[])
{
String s1="Sachin";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);
}
}
Output: SachinTendulkar