[Go to site: main page, start]

0% found this document useful (0 votes)
9 views9 pages

Java Programming Basics

The document provides a comprehensive overview of Java programming basics, including its features, data types, operators, and control structures. It includes code examples for fundamental concepts such as loops, methods, and object-oriented programming. Additionally, it presents sample programs demonstrating various functionalities like addition, checking even/odd, and calculating factorials.

Uploaded by

adik079787
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Java Programming Basics

The document provides a comprehensive overview of Java programming basics, including its features, data types, operators, and control structures. It includes code examples for fundamental concepts such as loops, methods, and object-oriented programming. Additionally, it presents sample programs demonstrating various functionalities like addition, checking even/odd, and calculating factorials.

Uploaded by

adik079787
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

submissions@[Link].

in
Java Training Test Assignment

Java Programming Basics


1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language.
It follows the rule:
“Write Once, Run Anywhere (WORA)”

2. Features of Java
 Simple – Easy to read and learn
 Object-Oriented – Uses classes and objects
 Platform Independent – JVM allows the same program to run on any system
 Secure – Strong memory management and access control
 Robust – Handles errors and exceptions well
 Multithreaded – Supports multiple tasks running at the same time
3. Basic Structure of a Java Program
class Hello {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Explanation:

 class Hello – Class declaration


 main() – Starting point of every Java program
 [Link]() – Prints text on the screen

4. Java Data Types


A. Primitive Data Types

Type Example Size Description


int 10 4 bytes Whole numbers
float 2.5f 4 bytes Decimal numbers
double 3.14 8 bytes Larger decimals
char 'A' 2 bytes Single character
boolean true/false 1 bit Logical values
byte 50 1 byte Small integers
short 1000 2 bytes Mid-range integers
long 999999L 8 bytes Very large integers

5. Variables in Java
Variables store data.

int age = 20;


float price = 99.50f;
String name = "Rahul";

6. Operators in Java
Arithmetic Operators
+ , - , * , / , %

Comparison Operators
== , != , > , < , >= , <=

Logical Operators
&& , || , !

7. Input in Java
Using Scanner class:

import [Link];

class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}

8. Conditional Statements
if-else
int age = 18;

if(age >= 18){


[Link]("Adult");
} else {
[Link]("Minor");
}

switch
switch(day){
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid Day");
}

9. Loops in Java
for loop
for(int i = 1; i <= 5; i++){
[Link](i);
}

while loop
int i = 1;
while(i <= 5){
[Link](i);
i++;
}

do-while
int i = 1;
do{
[Link](i);
i++;
} while(i <= 5);

10. Arrays
int[] numbers = {10, 20, 30, 40};
[Link](numbers[0]); // prints 10

11. Methods in Java


class Demo {
static void greet(){
[Link]("Hello!");
}

public static void main(String[] args){


greet();
}
}
12. Object-Oriented Programming Basics
Class and Object Example
class Student {
String name;
int age;

void display(){
[Link](name + " " + age);
}
}

class Test {
public static void main(String[] args){
Student s = new Student();
[Link] = "Ravi";
[Link] = 20;
[Link]();
}
}

13. Inheritance
class Animal {
void sound(){
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


void sound(){
[Link]("Dog barks");
}
}

14. Java Program Compilation & Execution


1. Write code → [Link]
2. Compile → javac [Link]
3. Run → java Hello
Hello World Program
class Hello {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

2. Add Two Numbers


class Add {
public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
[Link]("Sum = " + sum);
}
}

3. Check Even or Odd


class EvenOdd {
public static void main(String[] args) {
int num = 7;
if(num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}

4. Find Largest of Two Numbers


class Largest {
public static void main(String[] args) {
int a = 15, b = 25;

if(a > b)
[Link]("A is larger");
else
[Link]("B is larger");
}
}

5. Print 1 to 10 Using Loop


class Numbers {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++) {
[Link](i);
}
}
}

6. Factorial of a Number
class Factorial {
public static void main(String[] args) {
int n = 5;
int fact = 1;

for(int i = 1; i <= n; i++) {


fact *= i;
}

[Link]("Factorial = " + fact);


}
}
7. Fibonacci Series
class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1, c;

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

for(int i = 2; i < 10; i++) {


c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

8. Reverse a Number
class Reverse {
public static void main(String[] args) {
int num = 1234;
int rev = 0;

while(num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

[Link]("Reversed = " + rev);


}
}

9. Check Prime Number


class Prime {
public static void main(String[] args) {
int n = 7;
boolean isPrime = true;

for(int i = 2; i <= n/2; i++) {


if(n % i == 0) {
isPrime = false;
break;
}
}

if(isPrime)
[Link]("Prime");
else
[Link]("Not Prime");
}
}

10. Sum of Digits


class SumDigits {
public static void main(String[] args) {
int num = 1234;
int sum = 0;

while(num != 0) {
sum += num % 10;
num /= 10;
}

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


}
}

You might also like