[Go to site: main page, start]

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

Java Code Examples for Beginners

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)
4 views3 pages

Java Code Examples for Beginners

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

AComments.

java

public class AComments {


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

/* This is a
multi-line comment */

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

[Link]

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";

[Link]("Name: " + name);


[Link]("Age: " + age);
[Link]("Salary: " + salary);
[Link]("Grade: " + grade);
[Link]("Employed: " + isEmployed);
}
}

[Link]

public class CJavaOperatorsAddition {


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

[Link]("Sum: " + sum);


}
}

[Link]

public class DJavaOperatorsDivision {


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

[Link]("Division Result: " + result);


}
}

[Link]

public class EJavaOperatorsModulus {


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

[Link]("Remainder: " + remainder);


}
}

[Link]

public class FJavaRelationalOperators {


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

[Link]("x == y: " + (x == y));


[Link]("x != y: " + (x != y));
[Link]("x > y: " + (x > y));
[Link]("x < y: " + (x < y));
[Link]("x >= y: " + (x >= y));
[Link]("x <= y: " + (x <= y));
}
}

[Link]

public class GJavaLogicalOperator {


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

[Link]("a && b: " + (a && b));


[Link]("a || b: " + (a || b));
[Link]("!a: " + (!a));
}
}

[Link]

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

[Link]("Final value of x: " + x);


}
}

[Link]

public class IJavaIncrementDecrement {


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

[Link]("Original a: " + a);


[Link]("Post-increment a++: " + (a++));
[Link]("After post-increment: " + a);
[Link]("Pre-increment ++a: " + (++a));
[Link]("Post-decrement a--: " + (a--));
[Link]("After post-decrement: " + a);
[Link]("Pre-decrement --a: " + (--a));
}
}

[Link]

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;

[Link]("Implicit casting (int to double): " + b);


[Link]("Explicit casting (double to int): " + y);
}
}

You might also like