Professional Documents
Culture Documents
Chapter 2
Chapter 2
Ashok
Chapter - 2
• Variables
• Data Types
• Identifiers & Rules
• Reserved Words
• Java Coding Standards
• Java comments
• Reading Data From keyboard
12
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Variables
-> variables are used to store the data during program execution
-> We need to specify type of the variable to store the data
-> To specify type of data we will use 'data types'
-> To declare the variable in Java, we can use following syntax
Data types
-> Data types are used to specify type of the data
-> Data types are divided into 2 categories
1) Primitive Data Types
2) Non-Primitive Data Types
13
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Once a primitive data type has been declared its type can never change, although in most
cases its value can change. These eight primitive types can be put into four groups.
14
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
15
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Identifiers in Java
All Java components require names. Name used for classes, methods, interfaces and
variables are called Identifier.
Identifier must follow some rules. Here are the rules:
Rule-1 : The only allowed characters in java identifiers are:
➔ a to z
➔ A to Z
➔ 0 to 9
➔ $
➔ _ (underscore)
name -----> valid
name@ -----> invalid
age# ------> invalid
age ---→ valid
Rule-2 : Identifier should not start with digit (First letter shouldn’t be digit)
1age --------> invalid
age2 ------> valid
name3 -----> valid
_name -----> valid
$name ------> valid
@name ------> invalid
$_amt --------> valid
_1bill -----------> valid
Rule-3: Java reserved keywords cannot be used as an identifier (53 reserved words available
in java)
int byte = 20; -------> invalid bcz byte is a reserved word
byte for = 25; -------> invalid bcz for is a reserved word
int try = 30; ------> invalid bcz try is a reserved word
long phno = 797979799 -----> valid
16
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Rule - 5: Identifiers in Java are case sensitive; foo and Foo are two different identifiers.
Java Keywords
-> In Java we have total 53 reserved words, those are classified like below
17
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
18
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Note: Variables & Methods naming conventions are same. But methods will have
parenthesis ( ( ) ) variables will not have parenthesis.
19
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Java Comments
-> In java we have 3 types of comments
-> Commented code will not participate in compilation & execution
Note: Java compiler will skip commented lines of code
1) single line comments
Syntax:
// this is single line comment
2) multi line comments (when we don't want to compile multiple lines of code)
/*
commented code
*/
3) documentation comments (Used to generate API documentation)
/**
*
* @author ashok
*
*/
20
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
21
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
22
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok
Knowledge Check
1) What is full stack development?
2) Explain software project architecture
3) What are the roles & responsibilities of full stack developer
4) What is database and why we need it?
5) What is programming language & why we need programming language?
6) What is JAVA?
7) What are the features of java?
8) What is the difference between C and Java?
9) What type of applications we can develop using java & brief them
10) What is the difference between JDK, JRE and JVM?
11) What is the execution flow of java program?
12) What is the difference between interpreter and compiler?
13) Write JVM architecture & explain JVM components
14) What is JIT?
15) Write Java data types with size, range and default values
16) What is variable and how to create variables
17) Explain Java program elements
18) Write a java program to print welcome message
19) Write a java program on variables declaration, initialization
20) How many types of comments available in java
21) What is identifier and what are rules available for identifier
22) What are the reserved words in java
23) Write Java Naming Conventions for packages, classes, variables and methods
23
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com