Basic java program list
Basic java Error program
Program :- 1
class aman{
System.out.println("sonu");
}
System.out.println("sonu");
}
Program :- 2
class Demo
{
static void method ()
{
System.out.println("hello world");
}
public static void main(String args[])
{
Demo.method(); // without obj create create method call
}
}
C:\java>javac Testing.java
C:\java>java Demo
hello world
C:\java>
Program :- 3
class aman{
}
successful compiled
Program :- 4
class aman{
}
class java{}
successful compiled
Program :- 5
class aman{
}
class xyz {
public static void main(String args[]){
}
}
Program :- 6
class aman{
}
class xyz {
public static void main(String args[]){
System.out.println("welcome to java");
}
}
Program :- 7
class aman{
public static void main(String args[]){
}
}
class xyz {
}
Program :- 8
class aman{
public static void main(String args[]){
System.out.println("welcome to java");
}
}
class xyz {
}
print welcome to java
Program :- 9
Program :- 10
class aman{
public static void main(String args[]){
int a=10;
int b=20,c;
c= a+b;
System.out.println("sum of number is="+c);
}
}
class xyz {
}
Program :- 11
class student {
String name;
int age;
}
class xyz {
public static void main(String args[]){
student s1= new student();
s1.name="sonu";
s1.age=45;
}
Program :- 12
Program :- 13
class student {
String name;
int age;
}
class xyz {
public static void main(String args[]){
student s1= new student();
s1.name="sonu";
s1.age=45;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Program :- 14 Method related program
class aman{ static void show() { System.out.println("This is java language"); } }class xyz {public static void main(String args[]){ aman obj= new aman(); aman.show(); //Function
} }OutputC:\java>javac Testing.java
C:\java>java xyz
This is java language
C:\java>
class aman{
static void show()
{
System.out.println("This is java language");
}
}
class xyz {
public static void main(String args[]){
aman obj= new aman();
aman.show(); //Function
}
}
Output
C:\java>javac Testing.java
C:\java>java xyz
This is java language
C:\java>
Program :- 15
class Demo{
void show()
{
//yha pe kuch nahi likhg tab bhi program runn ho jarega
}
}
class mytest{
public static void main(String args[])
{
Demo obj = new Demo();
obj.show();
}
}
//Output
// C:\java>javac Testing.java
// C:\java>java mytest
// C:\java>
Program :- 16
class Demo{
public static void main(String[] args){
int a=5;
int b=7;
int c;
if(a>b){
c=a+b;
}
else
{
c=(a+b)*5;
}
int a1=2;
int b1=1;
int c1;
if(a1>b1){
c1=a1+b1;
}
else
{
c1=(a1+b1)*5;
}
System.out.println(c);
System.out.println(c1);
} }
C:\java>javac Testing.java
C:\java>java Demo
60
3
C:\java>







Comments
Post a Comment