i# First Program in Java (Hello, Java!)
// To import everything from package directory-[java >> lang ]
import java.lang.*;
// class name same as that of .java file
class HelloJava{
// main method
public static void main(String args[]){
System.out.println("Hello, Java!");
}
}
If programmer wants to use any pre-defined class than it should be imported in the program through import statement
Import statement makes the JVM go to the Java Standard Library, execute the code there and substitute the result ito the program
In this process, no code was copied into source program; hence no wastage of memory and processor time
"""
To run Program from CLI
[In: Input] :: [op: Output]
"""
[In] javac HelloJava.java
[In] java HelloJava.java
[op] Hello Java
class
is a keyword which is used for creating user defined datatype. Each and every java program starts its execution from main()
- Since
main()
method can be accessed by every java programme, hence its access specifier must bepublic
- Since
main()
method is executing only once throughout the life of entire java program and it is not called with any object hence nature ofmain()
is static
main()
method doesn't return anything value hence its datatype isvoid
-
To access
println()
method, we require an object of PrintStream class. -
An object of PrintStream class called
out
is created by SunMicro System in an another predefined class calledSystem
as a static data member. -
Hence, to access
println()
andprint()
method,System
class is used -
In
System.out.println()
: System is class; out is object; println() is method -
static
public void main (String args[]){}
is same aspublic
static void main(){}
-
String args[]
is same asString something_else[]
class Display{
public static void main(String args[]){
int a = 100;
System.out.println("Value of a is: " + a);
}
}
Value of a is: 100
class Sum{
public static void main(String args[]){
int a = 10;
int b = 80;
int c = a + b;
System.out.println("Sum of" + a "and" + b + "is" + c);
}
}
Sum of 10 and 80 is 90
Out of the following classes, the same class will run in the name of which the program has been saved
class One{
public static void main(){
class Two{
public static void main(){
// code here
}
}
}
}
class Test{
public static void main(){
System.out.println("First main method");
}
public static void main(){
System.out.println("Second main method");
}
}
// Filename: Demo.java
class First{
int a, b, c;
void accept(){
a = 100;
b = 900;
}
void sum(){
c = a + b;
}
void display(){
System.out.println("Sum is " + c);
}
}
class Demo{
public static void main(String args[]){
First obj1 = new First();
obj.accept();
obj.sum();
obj.display();
}
}
// Filename: Demo.java
class First{
int a, b, c; // data members
void accept(int x, int y){ // (x, y) :: local parameters
a = x;
b = y;
}
void sum(){
c = a + b;
}
void display(){
return c;
}
}
class Demo{
public static void main(String args[]){
First obj = new First();
obj.accept(99, 1);
obj.sum();
System.out.println(obj.display());
}
}
class Demo {
int x;
public static void main (String args[]){
System.out.println(x);
}
}
/*
Output:
Error...
*/