java_1
java_1
Chapter 5:
Java Programming -01
Java: Introduction
What is Java? 2 API: Application Programming Interface
Java : Methods
(class Name)
Java : System.out
Java Virtual Machine, or JVM, loads, verifies and executes Java bytecode. It is
known as the interpreter or the core of Java programming language because it
executes Java programming.
Java: Program
Program 1: print a message/text 6 Program 2 (ex): call main() of second
package CSE1203; package CSE1203;
public class First {
public static void main(String[] args) {
public class First {
System.out.println("Welcome to Java World");
public static void main(String[] args) { Second.main(null);
System.out.print("Welcome to Java World"); }
} }
}
Output
Output
Welcome to Java World
Welcome to Java World Talk less listen more
Notes: The class name must be the same as the .java Notes: In the First class just write the class name
source filename and package should be declared at first if you want to call other classes method
Memory Allocation
String class creates objects in String
Constant Pool (SCP) and only one
object is created for same literal
Java: Program
scanner class : next() method 11 scanner class : nextInt() method
package CSE1203;
import java.util.Scanner;
Built in Statements
if, switch, for, while,do-while
are the same as C/C++l
Java: Program
Local Variable and its Scope 12 Java: array declaration
public static void main(String[] args) { public static void main(String[] args) {
if(true) { int[] ax=new int[5];
int i=5; ax[0]=10; ax[1]=20;
System.out.println("i="+i); for(int i=0;i<5;i++)
} System.out.println(" "+ax[i]);
System.out.println("i="+i); }
} }
class Circle{
Point c;
int r;
static int count=0;
public
Circle(Point c,int r){
this.c=c;
this.r=r;
count++;
Java: static variable/method }
double Area() {
package CSE1203; return 2*3.14*r;
import java.awt.Point; }
Point getCenter() {
public class First { return c;
}
public static void main(String[] args) { static int getCount() {
Circle c1=new Circle(new Point(4,2),3); return count;
Circle c2=new Circle(new Point(1,2),4); }
System.out.println("Center="+c1.getCenter()); }
System.out.println("Area="+c1.Area());
System.out.println("Circle
Count="+c1.getCount());
}
Note: here count is a static variable and it
} is common for all objects. To return a
static variable a static method is used.
Java: static variable/method
Java: Visibility Modifiers 20 Java: Inner class
package CSE1203;
public class First {
Example: Visibility Modifiers public static void main(String[] args) {
CSE1203_01 CSE1203_02 //outer is Outer class object
Outer outer=new Outer();
outer.Display();
//in is Inner class object
Outer.Inner in = new Outer().new Inner(45);
in.getY();
}
}
THANK YOU