Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Core Java Mr.

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

Primitive Data Types


Primitive data types are divided into eight types

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.

Floating Data Types


This group includes float and double

Character data type


-> Character data type is used to store single character
-> Any single character (alphabet / digit / special character) we can store using 'char' data
type
-> char datatype will occupy 2 bytes of memory
-> When we are storing data into 'char' data type single quote is mandatory

14
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok

Boolean data type


-> It is used to store true or false values only
-> It will occupy 1 bit memory
Note: 8 bits = 1 byte
-> default value for boolean is false
Ex: boolean flag = false
Variables
-> Variables are used to store the data / value
-> To store the data into variable we need to specify data type
-> To store data into variables we need to perform 2 steps
1) Variable Declaration (defining variable with data type)
Ex: byte age ;
2) Variable Initialization (storing value into variable)
Ex: age = abc;
-> We can complete declaration and initialization in single line
byte age = 20;

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-4 : Spaces are not allowed in identifiers


int mobile bill = 400; // invalid
int mobile_bill = 400 ; // valid

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

Java coding/naming conventions


-> Java language followed some standards/conventions for pre-defined packages, classes
and methods....
-> Java language suggested java programmers also to follow same standards / conventions
-> Following these standards/conventions is not mandatory but highly recommended.
Following are different java coding conventions
1. coding convention for a class
-> Class name can contain any no.of words without spaces
-> Recommended to write every word first character as uppercase in class name

2. coding convention for interfaces


-> Class & Interface Naming conventions are same

3. coding convention for a variables


-> Variable name can have any no. of words without spaces
-> Recommended to start variable name with lowercase letter
-> If variable name contains multiple words, then recommended to write first word all
characters in lowercase and from second word onwards every word first character in
Uppercase

18
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok

4. coding convention for methods


-> Method name can have any no. of words without spaces
-> Recommended to start method name with lowercase letter
-> If method name contains multiple words, then recommended to write first word all
characters in lowercase and from second word onwards every word first character in
Uppercase

Note: Variables & Methods naming conventions are same. But methods will have
parenthesis ( ( ) ) variables will not have parenthesis.

5. coding convention for constants


-> Constant means fixed value (value will not change, it is fixed)
-> Recommended to write constant variable all characters in uppercase
-> If constant variable contains multiple words recommended to use _ (underscore) with all
uppercase characters

19
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok

6. coding convention for packages


-> Package name can have any no.of characters & any of words
-> Recommended to use only lowercase letters in package names
-> If package name contains multiple words then we will use . (dot) to separate words

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

String data type


-> String is a predefined class available in java.lang package
-> String can be used as a data type also
-> String is used to store group of characters
Note: Every java class can be used as a data type (referenced data type)
String name = "ashok";
String email = "ashok@gmail.com"
String country = "India";
String pwd = "ashoK@123";
-> To store data in String variable double quotes are mandatory
Reading Data from Keyboard
-> We can pass input for java program to perform some operation. Based on given input we
can execute some logic and we can send output.
Ex -1: Take 2 numbers as input and provide sum of those numbers
Ex- 2: Take first name & last name and provide full name

-> In java we have 2 classes to read data from Keyboard


1) java.io.BufferedReader
2) java.util.Scanner

21
Ashok IT, Phone: +91 9985396677 , Email: info@ashokitech.com, www.ashokitech.com
Core Java Mr. Ashok

// write a java program to perform sum of two numbers Using Scanner

// Write a java program to take two strings as input

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

You might also like