KEMBAR78
PDF1 Basic Java Programs | PDF | Object Oriented Programming | Computer Science
0% found this document useful (0 votes)
3 views12 pages

PDF1 Basic Java Programs

The document provides a series of basic Java programming examples covering fundamental concepts such as printing to the console, variable declaration, arithmetic operations, conditional statements, loops, arrays, methods, and command-line arguments. Each section includes a simple code snippet demonstrating the respective concept. This serves as a foundational guide for beginners learning Java programming.

Uploaded by

ramanjicse
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)
3 views12 pages

PDF1 Basic Java Programs

The document provides a series of basic Java programming examples covering fundamental concepts such as printing to the console, variable declaration, arithmetic operations, conditional statements, loops, arrays, methods, and command-line arguments. Each section includes a simple code snippet demonstrating the respective concept. This serves as a foundational guide for beginners learning Java programming.

Uploaded by

ramanjicse
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/ 12

Basic Java Programs

1. Hello World
Simple program to print Hello World.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Page 1 of 12
2. Variables and Types
Demonstrates primitive variables and printing.
public class VariablesExample {
public static void main(String[] args) {
int a = 10;
double b = 20.5;
boolean flag = true;
System.out.println("a=" + a + ", b=" + b + ", flag=" + flag);
}
}

Page 2 of 12
3. Arithmetic Operations
Shows arithmetic operators and result.
public class Arithmetic {
public static void main(String[] args) {
int x = 7, y = 3;
System.out.println("x+y=" + (x+y));
System.out.println("x*y=" + (x*y));
System.out.println("x/y=" + (x/y));
}
}

Page 3 of 12
4. If-Else
Basic conditional branching example.
public class IfElseExample {
public static void main(String[] args) {
int n = 5;
if (n % 2 == 0) System.out.println(n + " is even");
else System.out.println(n + " is odd");
}
}

Page 4 of 12
5. Switch Case
Using switch to choose action.
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Other day");
}
}
}

Page 5 of 12
6. For Loop
Iterate using for loop.
public class ForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}

Page 6 of 12
7. While Loop
Basic while loop example.
public class WhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}

Page 7 of 12
8. Do-While Loop
Executes body at least once.
public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}

Page 8 of 12
9. Arrays
Declare and iterate an array.
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {2,4,6,8};
for (int v : arr) System.out.println(v);
}
}

Page 9 of 12
10. Enhanced For
Use enhanced for-loop (for-each).
public class EnhancedFor {
public static void main(String[] args) {
String[] names = {"Alice","Bob","Carol"};
for (String s : names) System.out.println(s);
}
}

Page 10 of 12
11. Methods
Define and call a method.
public class MethodsExample {
public static void main(String[] args) {
System.out.println(sum(4,5));
}
static int sum(int a, int b) { return a + b; }
}

Page 11 of 12
12. Command-line Args
Read args from command line.
public class CommandLineArgs {
public static void main(String[] args) {
for (String arg : args) System.out.println(arg);
}
}

Page 12 of 12

You might also like