0% found this document useful (0 votes)
6 views3 pages

Java Code Examples

The document contains multiple Java classes demonstrating various programming concepts including comments, variables, arithmetic operations, relational and logical operators, assignment operations, increment/decrement operations, and type casting. Each class includes a main method that outputs relevant information to the console. The examples serve as educational snippets for understanding basic Java syntax and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Code Examples

The document contains multiple Java classes demonstrating various programming concepts including comments, variables, arithmetic operations, relational and logical operators, assignment operations, increment/decrement operations, and type casting. Each class includes a main method that outputs relevant information to the console. The examples serve as educational snippets for understanding basic Java syntax and functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AComments.

java

public class AComments {


public static void main(String[] args) {
// This is a single-line comment
System.out.println("Hello, Comments!");

/* This is a
multi-line comment */

/**
* This is a documentation comment
* used to describe classes and methods
*/
}
}

BVariables.java

public class BVariables {


public static void main(String[] args) {
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isEmployed = true;
String name = "Perera";

System.out.println("Name: " + name);


System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Employed: " + isEmployed);
}
}

CJavaOperatorsAddition.java

public class CJavaOperatorsAddition {


public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;

System.out.println("Sum: " + sum);


}
}

DJavaOperatorsDivision.java

public class DJavaOperatorsDivision {


public static void main(String[] args) {
int a = 20;
int b = 4;
int result = a / b;

System.out.println("Division Result: " + result);


}
}

EJavaOperatorsModulus.java

public class EJavaOperatorsModulus {


public static void main(String[] args) {
int a = 20;
int b = 6;
int remainder = a % b;

System.out.println("Remainder: " + remainder);


}
}

FJavaRelationalOperators.java

public class FJavaRelationalOperators {


public static void main(String[] args) {
int x = 10, y = 20;

System.out.println("x == y: " + (x == y));


System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x >= y: " + (x >= y));
System.out.println("x <= y: " + (x <= y));
}
}

GJavaLogicalOperator.java

public class GJavaLogicalOperator {


public static void main(String[] args) {
boolean a = true;
boolean b = false;

System.out.println("a && b: " + (a && b));


System.out.println("a || b: " + (a || b));
System.out.println("!a: " + (!a));
}
}

HJavaAssignmentOperator.java

public class HJavaAssignmentOperator {


public static void main(String[] args) {
int x = 10;
x += 5; // x = x + 5
x -= 2; // x = x - 2
x *= 3; // x = x * 3
x /= 4; // x = x / 4

System.out.println("Final value of x: " + x);


}
}

IJavaIncrementDecrement.java

public class IJavaIncrementDecrement {


public static void main(String[] args) {
int a = 5;

System.out.println("Original a: " + a);


System.out.println("Post-increment a++: " + (a++));
System.out.println("After post-increment: " + a);
System.out.println("Pre-increment ++a: " + (++a));
System.out.println("Post-decrement a--: " + (a--));
System.out.println("After post-decrement: " + a);
System.out.println("Pre-decrement --a: " + (--a));
}
}

JJavaCasting.java

public class JJavaCasting {


public static void main(String[] args) {
// Implicit casting
int a = 10;
double b = a;

// Explicit casting
double x = 9.78;
int y = (int) x;

System.out.println("Implicit casting (int to double): " + b);


System.out.println("Explicit casting (double to int): " + y);
}
}

You might also like