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

Java Code Examples

The document contains multiple Java code snippets demonstrating various programming concepts including comments, variable declarations, arithmetic operators, relational operators, logical operators, assignment operators, increment/decrement operations, and type casting. Each section features code examples that illustrate how to use these concepts in practice. Overall, it serves as a basic introduction to Java programming syntax and operations.
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 code snippets demonstrating various programming concepts including comments, variable declarations, arithmetic operators, relational operators, logical operators, assignment operators, increment/decrement operations, and type casting. Each section features code examples that illustrate how to use these concepts in practice. Overall, it serves as a basic introduction to Java programming syntax and operations.
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
// 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
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
int a = 10;
int b = 20;
int sum = a + b;

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

DJavaOperatorsDivision.java
int a = 20;
int b = 4;
int result = a / b;

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

EJavaOperatorsModulus.java
int a = 20;
int b = 6;
int remainder = a % b;

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

FJavaRelationalOperators.java
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
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
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
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
// 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