| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
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!");
}
}"""
To run Program from CLI
[In: Input] :: [op: Output]
"""
[In] javac HelloJava.java
[In] java HelloJava.java
[op] Hello JavaTo 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 called System as a static data member.
Hence, to access println() and print() 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 as public static void main(){}
String args[] is same as String 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: 100class 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 90class 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...
*/| Back | FazBrowse Home | New Git URL |