The document contains code snippets demonstrating basic Java programming concepts like printing "Hello World", adding two integers, using arithmetic operators, finding the ASCII value of a character, swapping two numbers, and reversing an integer. Each code sample shows how to implement the concept by declaring variables, using the relevant operators or methods, and printing output to demonstrate the result.
1 of 4
Download to read offline
More Related Content
OBJECT ORIENTED PROGRAMMIING LANGUAGE PROGRAMS
1. HELLO WORLD
class HelloWorld {
// Your program begins with a
call to main().
// Prints "Hello, World" to the
terminal window.
public static void main(String
args[])
{
System.out.println("Hello,
World");
}
}
ADDING OF TWO INTEGERS
import.java.io.*;
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
System.out.println(first + " + " +
second + " = " + sum);
}
}
2. operators:
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
}
}
ASCII VALUE OF THE CHARACTER
public class AsciiValue {
public static void main(String[] args) {
char ch = 'a';
int ascii = ch;
// You can also cast char to int
int castAscii = (int) ch;
System.out.println("The ASCII value of "
+ ch + " is: " + ascii);
System.out.println("The ASCII value of " +
ch + " is: +castAscii);
}
}
3. SWAPPING OF TWO NUMBERS
public class SwapNumbers {
public static void main(String[] args) {
float first = 1.20f, second = 2.45f;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
// Value of first is assigned to temporary
float temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first)
is assigned to second
second = temporary;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
4. Class reverse {
Public static int reverse( int num)
{
Int reverse=0;
While(num!=0)
{
Reverse=reverse*10+num%10;
Num/=10;
}
Return reverse;
}
}