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

Print Multiple Variables in Java

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 views6 pages

Print Multiple Variables in Java

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

Java Programming Notes: Variables, Constants,

Data Types, Input and Output


1. What is a Variable?

A variable in Java is a reference to a memory location used to store data. You


can store, modify, and reuse data by referencing these variables.

Example:
int age = 20;

• int: Data type (integer)


• age: Variable name
• 20: Value assigned to the variable

2. Variable Declaration and Initialization

You can declare a variable and assign it a value in one step. You can also
declare multiple variables in one line.

Example:
int num = 10; // Declares and assigns value in one step

Note: You can declare multiple variables in one line:


int a, b, c;

3. What is a Constant?

In Java, constants are declared using the final keyword. Once a value is
assigned to a constant, it cannot be changed.

Example:
final float pi = 3.1416f;

• final: Keyword indicating the value cannot be changed.


• float: Data type for decimal numbers.
• pi: Constant name.
4. Data Types

Data types in Java specify the kind of value a variable can hold. Here are the
common data types:

Type Explanation Examples


int Integer numbers 5, -100, 0
float Decimal numbers 3.14, -2.5
char A single character 'A', 'z'
double Bigger/more precise decimals 3.1415926535

Size and Range in Java (for 32-bit systems):

Type Size Range


int 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes ±3.4e38
double 8 bytes ±1.7e308
char 2 bytes 0 to 65,535 (Unicode)

5. Taking Input — Scanner Class

In Java, you use the Scanner class to take input from the user.

Example:
import [Link];

Scanner scanner = new Scanner([Link]);


int age = [Link]();

• nextInt(): Reads an integer from the user input.

Common input methods for different data types:

• int: nextInt()
• float: nextFloat()
• char: next().charAt(0) (for single characters)
• double: nextDouble()
• string: next() (for words)
6. Rules for Naming Variables

In Java, variable names must follow these rules:

• Variable names cannot start with a number.


• They can contain letters, numbers, and underscores.
• Reserved keywords cannot be used.

Good Practices:

• Use camelCase naming style for variables (e.g., studentAge,


totalSalary).
• Choose meaningful variable names (e.g., radius, averageMarks).

7. Example Program (Sum of Two Numbers)


import [Link];

public class SumTwoNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int a, b, sum;
[Link]("Enter two numbers: ");
a = [Link]();
b = [Link]();
sum = a + b;
[Link]("Sum = " + sum);
}
}

Input:
Enter two numbers: 5 7

Output:
Sum = 12
8. Another Example (Average of Two Decimal Numbers)
import [Link];

public class AverageTwoNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
float x, y, avg;
[Link]("Enter two numbers: ");
x = [Link]();
y = [Link]();
avg = (x + y) / 2;
[Link]("Average = %.2f\n", avg);
}
}

• The %.2f format specifier ensures that the result is displayed with two
decimal places.

9. [Link]() — Explanation

[Link]() is used to print text, data, or variables to the console


in Java.

• System: Refers to the System class in the [Link] package.


• out: out is an instance of PrintStream, used to send output to the
console.
• println(): This method prints the content and moves to the next line.

Example:
[Link]("Hello, World!");

Output:
Hello, World!
Key Points:

• The println() method prints the content and moves the cursor to the
next line.
• You can print numbers, variables, or expressions.

Examples of [Link]() in Action:

1. Printing a String:
[Link]("Hello, World!");

Output:
Hello, World!

2. Printing Numbers:
int age = 20;
[Link](age);

Output:
20

3. Printing Multiple Values:


int a = 5, b = 7;
[Link]("Sum of a and b: " + (a + b));

Output:
Sum of a and b: 12

4. Printing Expressions:
[Link](10 + 5);

Output:
15

5. Printing a Variable:
String name = "Nafis";
[Link]("Name: " + name);
Output:
Name: Nafis

Comparison Between println() and print()

• println(): Prints the content and moves to a new line.


• print(): Prints the content without moving to a new line.

Example:
[Link]("Hello");
[Link](" ");
[Link]("World!");

Output:
Hello World!

You might also like