[Go to site: main page, start]

0% found this document useful (0 votes)
12 views8 pages

Java Programming Basics and Examples

This document provides an overview of Java programming, including basic syntax, compilation and execution steps, and the structure of Java code. It covers key concepts such as classes, methods, variables, data types, and user input handling using the Scanner class. Additionally, it explains the differences between print() and println(), as well as conditional statements.
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)
12 views8 pages

Java Programming Basics and Examples

This document provides an overview of Java programming, including basic syntax, compilation and execution steps, and the structure of Java code. It covers key concepts such as classes, methods, variables, data types, and user input handling using the Scanner class. Additionally, it explains the differences between print() and println(), as well as conditional statements.
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

Java Notes

Hello World!

class FirstClass {

public static void main(String args[]){

[Link]("Hello World!");

OP: Hello World!

Java code runs in 2 Steps i.e.,

Step 1 – Compilation

Byte code
Source Compiler (Cross
Code .jav (Present System
a in JDK) Executabl
e)

Step 2 – Execution

Nativ
Byte
JVM e
Code
Code

Components of Code:
 Function or Method: It is a block of code which takes input, performs some work
and returns some output.

Note: main (Executes at First)

 Class: It is a group of objects which have common properties. A class can have
some properties and functions (also called as Methods)

Print() vs Println()

 Print() – prints the given in a line and if there is another line of output that need
to be printed then it will append it in the same line.
 Println() – prints the given in a line and if there is another line of output that
need to be printed then it will print it in the next line.

public class SecondClass{

public static void main(String[] args){

//Output

[Link]("Hello, World! with Java");

[Link]("Hello, World! with Java");

OP: Hello, World! with Java

Hello, World! with Java

public class SecondClass{

public static void main(String[] args){

//Output

[Link]("Hello, World! with Java");

[Link]("Hello, World! with Java");

OP: Hello, World! with JavaHello, World! with Java

 \n can also be used to do the same thing as println() method

public class SecondClass{


public static void main(String[] args){

//Output

[Link]("Hello, World! with Java\n");

[Link]("Hello, World! with Java");

OP: Hello, World! with Java

Hello, World! with Java

Q)Print the Pattern:

**

***

****

public class StarPattern{

public static void main(String[] args){

[Link]("*\n**\n***\n****");

Variables in Java:

Variables are like containers which stores data in them making it easy to make
repeated use of the data (Each variable should be given a unique name) and If we
want we can change the data stored inside the variables as per our requirement.

Java is a typed language i.e., we have to specify the datatype before defining the
variable

Datatypes in Java: These are declarations for the type of variables(Determines size &
type of the data associated with variables)

 Primitive: These are data types of fixed size and used to store Simple values

1. byte
2. short
3. char
4. boolean
5. int
6. long
7. float
8. double

 Non-Primitive: These are data types of variable size and used to store Complex
values (Usually declared with a ‘new’ keyword [Similar to JS])

String Object Class

Array Interface

Impt. Observations:

 Java adds strings to numbers without throwing any error

 If it detects integer - integer or integer - float addition then it performs


arithmetic addition

 Likewise if it detects string - string addition then it appends them both

 Likewise if it detects string – boolean addition then it appends them both


 But when it detects integer – Boolean or double – Boolean addition then it
throws an error

 Operators priority: () > * > / > % > - > +

General Code

public class SecondClass{

public static void main(String[] args) {

int a = 25;

int b = 10;

int sum = a+b;

int diff = a-b;

int multiply = a*b;

int divide = a/b;

int remainder = a%b;

[Link](sum); //op = 35

[Link](diff); //op = 15

[Link](multiply); //op = 250

[Link](divide); //op = 2

[Link](remainder); //op = 5

}
Taking Dynamic (User - Driven) Input in Java:

import [Link];

public class SecondClass {

public static void main(String[] args) {

// Create a Scanner object to read input from the console

Scanner scanner = new Scanner([Link]);

// Prompt the user to enter their name

[Link]("Enter your name: ");

// Read the user's input as a String

String name = [Link]();

// Prompt the user to enter their age

[Link]("Enter your age: ");

// Read the user's input as an integer

int age = [Link]();

// Display the user's input

[Link]("Hello, " + name + "! You are " + age + " years old.");

// Close the scanner

[Link]();

Instead of nextLine or nextInt if we use next then it takes only one token

i.e., if we give input as ‘tony stark’ then it will only consider ‘tony’
Top Observations:

 We need Scanner class (built-in) to read user input from the keyboard.

 Scanner scanner = new Scanner([Link]);


Creates a tool or object (scanner) to read user input.

 nextLine(): Reads all text until the user presses Enter.

 nextInt(): Specifically looks for a whole number.(That too in nxt ln)

 [Link](); releases system resources when code is exited.

#Area of Circle:

import [Link];

public class SecondClass {

public static void main(String[] args) {

Scanner tool = new Scanner([Link]);

[Link]("Enter the Radius of Circle: ");

double radius = [Link](); //IP: 2

double area = (3.14*radius*radius);

[Link]("The Area is: "+ area); //OP: 12.56

[Link]();

#Area of Rectangle:

import [Link].*;

public class SecondClass{

public static void main(String[] args) {

Scanner tool = new Scanner([Link]);

[Link]("Enter the Breadth of Rectangle: ");

double breadth = [Link]();//IP:25

[Link]("Enter the Length of rectangle: ");

double length = [Link]();//IP:49


double area = (length*breadth);

[Link]("The Area of rectangle : " + area);//OP:1225.

[Link]();

#Volume of Cube

import [Link].*;

public class SecondClass{

public static void main(String[] args) {

Scanner tool = new Scanner([Link]);

[Link]("Enter the length of cube: ");

double length = [Link]();

double volume = (length*length*length);

[Link]("The volume of Cube is: " +volume);

[Link]();

Conditional Statements:

 if-else statement

Syntax: if(condition){

//Code to be Executed

} else{

//Code to be Executed

Note: if we want to check equality then we should use two == symbols.

You might also like