java

Simple Java language programs

simple Java language programs

In this tutorial, we will learn some of simple Java language programs

We are going to learn some simple Java programs and how to write those programs for beginners.

Java program has three segments

  1. Class declaration
  2. pre define method(main method) definition
  3. Print statements

Example

public class My_First_Java_Program //class declaration with class name
{
public static void main(String args[])  //main method – built-in method
{
        System.out.print(“my first java program”); //print statement
      }

}

 

This  Java program display a string statements
When the above code is executed it produces the following result
my first java program

How to write my first java program

Let,s look at  how to save the file, compile and run this program  please follow these steps

  • Open the Notepad++ and add the  above code
  • Save the file,
  • file name My_First_Java_Program.java
  • the file name must be the same as the class name
Example
  • Open the command prompt
  • go to the directory
  • fine where you saved the file.

The explanation is given below

  • Type ‘ javac My_First_Java_Program.java’ and press enter to compile your code. If there are no error in your code, the commend prompt Will take you to the next line
Compile the program
  • Then, type   ‘ java My_First_Java_Program’ to run your program and enter the enter key
Run the program
  • You will be able to see ‘my first java program’ printed on the window
Output

 

out put – my first java program

Explanation of main method in Java

We use public static void main String args[]. Each word has different purpose and meaning

public : the public is an Access Modifier, it defines who can access this method. public means that this method will be accessable by any class. that means if other classs are able to access this class

static : it is a keyword in java, which is used several ways in java.

void: it is used to define the return type of the method. it defines what the method can returm. Viod means the Method will not return any value. (one of the return type int, which returns any value)

main: it means it is a strating point for an application in java. It is named in method

String args[]: it is parametter to the main method.

Example programs

2 nd program

This is second and simple  java program this program display a byte value
first, assign a variable mynewmarks in byte then assign a value 10 to mynewmarks
then call display mynewmarks – 10

my first java program

 

public class Mynewmarks  //class declaration
{
public static void main(String args[]) //main method
{
byte mynewmarks;  //variable declaration
mynewmarks=10;  //value initialization to variable
System.out.print(mynewmarks);// display statement
}
}

When the above code is executed it produces the following result

out put - 10

Some program Examples given below

1 – This program provides a character output

public class alphaphat{
public static void main(String args[]){
char ch='A';
System.out.print(ch);
}
}

 

output - A
2 – This program provides a character output using ASCII value

public class char1{
public static void main(String args[]){
char ch=67;
System.out.print(ch);
}
}

 

output  - c
3

public class doublealphapat{
public static void main(String args[]){
char ch=’67’;  // It is an ascii value, not a charater
System.out.print(ch);
}
}
out put  – error
4 – This program displays octal value

public class octal{
public static void main(String args[]){
char ch=067;
System.out.print(ch);
}
}

out put  – 7

5

public class Display1{
public static void main(String args[]){
char ch=’65535;
System.out.print(ch);
}
}
out put  – ?
6

public class display2{
public static void main(String args[]){
char ch=’u0015′;
System.out.print(ch);
}
}
output  – symbol
7

public class displayChar{
public static void main(String args[]){
char ch=’A’;
System.out.print(ch+1);
}
}
output  – 66
public class concatenate{
public static void main(String args[]){
char ch=’A’;
System.out.print(“my charecter is “+ch+1);
}
}

out put  – my charecter is A1

8
public class alphapat2{
public static void main(String args[]){
char ch=0x67;
System.out.print(ch);
}
}

output  – g

9
public class alphapat3{
public static void main(String args[]){
char ch=’A’;
System.out.print((char)(ch+1));
}
}
output  – B

 

print bio data using Java program

class biodata{
public static void main(String args[]){
String fname;
String lname;
String fullname;
int age;
char bloodgroup;
double salary;
double height;
String game;
char divition;
int grade;

fname="karmehavannan";
lname="sathasivam";
age=20;
bloodgroup='A';
salary=20000;
height=163;
grade=12;
divition='B';
game="football";

System.out.println("my first name is"+" "+fname);
System.out.println("my last name is" +" " +lname);
System.out.println("my full name is" +" " +fname+" "+lname);
System.out.println("my age is"+" "+age);
System.out.println("my salary is"+" "+salary);
System.out.println("my blood group is"+" "+bloodgroup);
System.out.println("my height is"+" "+height);
System.out.println("my fevorite game is"+" "+game);
System.out.println("my Grade is"+" "+grade+divition);

}

}

When the above code is executed , it produces the following result

boolean 

1. public class vanbuolean{
public static void main(String args[]){
boolean bul;
bul =true;
System.out.print(bul);
}
}
out put – true

Funny program

public class face1{
public class doublealphapat{
public static void main(String args[]){
char ch=2;
System.out.print(ch);
}
}

out put  – face

 

Aarethmatic oparation

program 1

class add{
public static void main(String args[]){
int x=10;
double y=3;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
}
}

 


output

13.0
7.0
30.0
3.33333333333335
1.0


program 2

class oper{
public static void main (String arg[]){
int a =3;
int b = 5;
int c = 1;
a= a+b+c;

System.out.println("__________________________");
System.out.println(a);
System.out.println(b);
System.out.println(c);

b+=a;
System.out.println("__________________________");
System.out.println(a);
System.out.println(b);
System.out.println(c);
c=b+++a--+--c;
System.out.println("__________________________");
System.out.println(a);
System.out.println(b);
System.out.println(c);
a=--b+--c+a++;
System.out.println("__________________________");
System.out.println(a);
System.out.println(b);
System.out.println(c);

}
}

When the above code is executed it produces the following result

.................................................
9
5
1
..................................................
9
14
1
....................................................
8
15
23
.....................................................
44
14
22
......................................................



Unary operation

class add1{
public static void main(String args[]){
int x=10;
int y=3;

System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
System.out.println(x-=1);
System.out.println(x+1);
System.out.println(x-1);
System.out.println(x+=1);
System.out.println(x-=1);
System.out.println(++x);
System.out.println(y);
System.out.println(++y);
System.out.println(y++);
System.out.println(y);
System.out.println(++y);
System.out.println(++y+y);
System.out.println(++y + ++y);
System.out.println(++y + ++y+y++);
}
}

 

When the above code is executed it produces the following result

13
7
30
3
1
9
10
8
10
9
10
3
4
4
5
6
14
17
32
Suggested for you
Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.