2. public class ArithmeticDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = x + y;
System.out.println("x + y = " + result);
result = x - y;
System.out.println("x - y = " + result);
result = x * y;
System.out.println("x * y = " + result);
result = y / x;
System.out.println("y / x = " + result);
result = x % 3;
System.out.println("x % 3 = " + result);
}
}
ARITHMETIC OPERATOR
3. UNARY OPERATOR
public class UnaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = +x;
System.out.println("+x = " + result);
result = -y;
System.out.println("-y = " + result);
result = ++x;
System.out.println("++x = " + result);
result = --y;
System.out.println("--y = " + result);
boolean ok = false;
System.out.println(ok);
System.out.println(!ok);
}
}
4. ARRAYS
• Normally, array is a collection of similar type
of elements that have contiguous memory
location.
• Java array is an object the contains elements
of similar data type.
• We can store only fixed set of elements in a
java array.
• Array in java is index based, first element of
the array is stored at 0 index
6. class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
7. class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5}; nitialization
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
8. Two Dimensional Array
class Testarray3{
public static void main(String args[]){
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();
}
}}
9. Java Packages
A java package is a group of similar types of
classes, interfaces and sub-packages.
Package in java can be categorized in two form,
1. built-in package
2.user-defined package.
There are many built-in packages such as java,
lang, awt, javax, swing, net, io, util, sql etc.
Here we see user-defined packages.
10. Advantage of Java Package
1) Java package is used to categorize the classes
and interfaces so that they can be easily
maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
How to run java package program
• To Compile: javac -d . Simple.java
• Run: java mypack.Simple
11. Example of package that import the
packagename.*
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
12. Example of package by import
package.classname
//save by A.java
package pack;
public class A{
public void msg()
{
System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
13. JavaDoc comments
• In general, Javadoc comments are any multi-
line comments ("/** ... */") that are placed
before class, field, or method declarations.
• /** * Represents a student enrolled in the
school. * A student can be enrolled in many
courses. */