[go: up one dir, main page]

0% found this document useful (0 votes)
3 views15 pages

Variables

The document covers fundamental concepts in Java programming, including keywords, variables, identifiers, data types, declaration, and initialization. It explains the rules for naming variables and identifiers, as well as details about primitive and reference data types. Additionally, it provides examples and poses questions related to the Java Development Kit (JDK), Java Virtual Machine (JVM), and Java Runtime Environment (JRE).

Uploaded by

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

Variables

The document covers fundamental concepts in Java programming, including keywords, variables, identifiers, data types, declaration, and initialization. It explains the rules for naming variables and identifiers, as well as details about primitive and reference data types. Additionally, it provides examples and poses questions related to the Java Development Kit (JDK), Java Virtual Machine (JVM), and Java Runtime Environment (JRE).

Uploaded by

ml4033676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Day 3

• Keyword
• Variable
• Identifier
• Data type
• Declaration and Initialization
Keyword
• Reserved words in Java with special meanings.
• Example: int, class, if, return, static, void , public.

They are predefined by the language and have special purposes. You
cannot use them for naming variables, methods, or classes.
class Demo {
public static void main(String[] args) {
int age = 25;
}}
• Declaration: • Initialization:

• Declaring a variable without • Assigning a value to a declared


assigning a value. variable.

• int age; // Declaration = • age = 25; // Initialization

• int score = 100; // Declaration and


Initialization combined
Variables
• A variable is a container used to store data in a program. Each
variable has a name and a data type.
example:
int age = 25;
String name = "John";

System.out.println(Name);
System.out.println(age);
general rules for naming variables

•Names must begin with a letter or $ and _


•Names should start with a lowercase letter, and cannot contain whitespace
•Names can contain letters, digits, underscores, and dollar signs
•Names are case-sensitive ("myVar" and "myvar" are different variables)
•Reserved words (like Java keywords, such as int or boolean) cannot be used as names

Which is correct:
1.Int Number=10;
2.String value=hello everyone;
3.boolean x=true;
4.String str value=“Vanakkam”;
Identifier
• The name used for variables, methods, classes, etc.

• Example: Demo, main, age.

• Rules for Identifiers:


• Must start with a letter, _, or $.
• Cannot use spaces or special symbols.
• Cannot use keywords.
• public class Demo { // 'Demo' is an identifier for the class
• public static void main(String[] args) { // 'main' is an identifier for
the method
• int age = 25; // 'age' is an identifier for the variable
• }
•}

DataType  A data type defines the type of data a


variable can hold.
Data Type • public class Demo {
• public static void main(String[]
Primitive
args) {
• Byte ,short, int, long, float, int age = 30; // Integer type

double, char, boolean. float salary = 25000.50f; // Float type


String name = "Alice"; // String type
• int: Stores whole numbers like 1, 25. boolean isEmployee = true; // Boolean type
• float: Stores decimal numbers like
System.out.println("Name: " + name +
3.14.
", Age: " + age);
• String: Stores text like "Hello".
• boolean: Stores true or false.
• }
•}
Reference DataType
Reference Data Types: Objects like String, array, class and
interface and object
Primitive Datatype

Byte Short
• Size:8bits(1 byte) • Size:16 bits(2 bytes)
• Range : -128 to 127 • Range : -32,768 to 32,767
• Default value :0 • Default value :0

Byte b=100; Byte b=1000;


Primitive Datatype

int long
• Size:32 bits(4 bytes) • Size:64 bits(8 bytes)
• Range : -2,147,483,648 to • Range :-2^63 to 2^63-1
2,147,483,647 • Wider range is needed
• Default value :0
• Most Commonly Used. long l=1000000000L;

int b=100000;
Primitive Datatype

float double
• Size:32 bits(4 bytes) • Size:64 bits(8 bytes)
• Default value :0.0f • Default value :0.0d
• 7 decimal digits • 15 to 16 decimal digits

Float f=10.5f; Double d=20.45;


Primitive Datatype

char boolean
• Size:16 bits(2 bytes) • Size:1bit
• Single charactor • Values: true or false
• Default value :0
Char c=‘A’; • Wider range is needed

boolean flag=true;
Summary
• Concept Definition Example
• Keyword Reserved word with special meaning. int, class, if
• Variable A container to store data.  int age = 25;
• Identifier Name for variables, methods, or classes. Demo, age, main
• Data Type Defines the type of data a variable can hold . int, String, boolean
• Declaration Declaration is defining a variable;  int age;
• Initialization initialization is giving it a value. age = 25;
Questions
1.What is JDK & Uses?
2.What is JVM & Uses?
3.What is JRE ?
4.What is Java Platform?
5.Java is Independent Platform or not and Why?
6.What is Keywords ?
7.What is Variables?
8.What is DataType and Tell some Basic types?
9.Explain Combined Declaration and Initialization with examples?

You might also like