CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
Java unit 2
1. INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
UNIT-2
[Naming Conventions in Java. Data types in Java, Literals]
Naming Conventions in Java
Java naming convention is a rule to follow as you decide what to name your identifiers such as
class, package, variable, constant, method etc. But, it is not forced to follow. So, it is known as
convention not rule.
Below are some naming conventions of for java programming language, which must be followed
while developing software in java for good maintenance and readability of code. Java uses
CamelCase as a practice for writing names of methods, variables, classes, packages and constants.
If name is combined with two words, second word will start with uppercase letter always e.g.
actionPerformed(), firstName, etc.
1. Classes and Interfaces:
● Class names should be nouns, in mixed case with the first letter of each internal word
capitalized. Interfaces name should also be capitalized just like class names.
● Use whole words and must avoid acronyms and abbreviations.
Examples:
class CseStudent
Interface FootBall
2. Methods :
● Methods should be verbs, in mixed case with the first letter lowercase and with the first
letter of each internal word capitalized.
Examples:
void speedUp(int increment)
3. Variables :
● Variable names should be short yet meaningful.
● Should not start with underscore(‘_’) or dollar sign ‘$’ characters.
● Should be mnemonic i.e, designed to indicate the intent of its use.
● One-character variable names should be avoided except for temporary variables, such as
i, j, k, m, and n for integers; c, d, and e for characters.
Examples:
int rollNo;
4. Constant variables:
● Should be all uppercase with words separated by underscores (“_”).
● There are various constants used in predefined classes like Float, Long, String etc.
Examples:
static final int MIN_WIDTH = 4;
Provided By Shipra Swati
2. INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
5. Packages:
● The prefix of a unique package name is always written in all-lowercase ASCII lettersand
should be one of the top-level domain names, like com, edu, gov, mil, net, org.
● Subsequent components of the package name vary according to an organization’s own
internal naming conventions.
Examples:
com.sun.eng
Data Types in java
Data type is a special type of keyword which is useful for allocating sufficient memory for that data.
In other words, Java Data Types are the classification, to identify one of various types of data such
as real number, integer, floating point or boolean. That determines the possible values for that
particular type and the operations that can be done on values of that type.
So, Java data types are in various forms to store the different types of information. These Java data
types are mainly classified into 2 types.
(a) Primitive Data Types and
(b) Non-Primitive Data Types.
Java data types can not be modified once they declared.
Provided By Shipra Swati
3. INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
The primitive data types in Java are also called as Fundamental data types or Basic data types. In
primitive, Java data types are mainly classified into four types. They are Integer Data Types In Java,
Floating-Point Data Types In Java, Character Data Types in Java and Boolean Data Types in Java.
Following table shows the details related to primitive data types:
In Non-Primitive, Java data types are further classified into three types. User defined data type is
one of these Non-primitive Java data types. According to the requirement we can choose the
appropriate type in Java data types.
There are three types of variables in java:
• local variable
• instance variable
• static variable
1) Local Variable: A variable which is declared inside the method is called local variable.
2) Instance Variable: A variable which is declared inside the class but outside the method, is called
instance variable. It is not declared as static.
3) Static variable: A variable that is declared as static is called static variable. It cannot be local.
Provided By Shipra Swati
4. INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
Literals
Literals in Java are a sequence of characters (digits, letters, and other characters) that represent
constant values to be stored in variables. Java language specifies five major types of literals.
1. Integer Literals: Java has several integer literals: int, long, byte, and short. We can specify
the integers either in decimal, hexadecimal or octal format. To indicate a decimal format
put the left most digit as nonzero. Similarly use the prefix ox to indicate hexadecimal and to
indicate the octal format put a zero digit followed by the digits 0 to 7.
Examples:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
An integer literal larger than an int can hold is automatically considered to be of the type
long. You also can indicate that a literal should be a long integer by adding the letter L (L or
l) to the number.
2. Floating-point Literals: Floating-point literals use a period character (.) for the decimal
point. Java has two kinds of floating-point numbers: float and double. The default type when
you write a floating-point literal is double. However, the suffix F (or f) is appended to
designate the data type of a floating-point literal as float.
Examples:
double myGPA = 2.25;
float piValue = 3.1415927F;
We can also specify a floating-point literal in scientific notation using Exponent (E or e). For
instance: the double literal 0.0314E2 is interpreted as: 0.0314 *10² (i.e 3.14).
3. Character Literals: Character literals are expressed by a single character surrounded by
single quotation marks, such as 'a', '#', and '3'. Some character literals represent characters
that are not readily printable or accessible through a keyboard. Following Table lists the
special codes that can represent these special characters as well as characters from the
Unicode character set. The letter d in the octal, hex, and Unicode escape codes represents a
number or a hexadecimal digit (a–f or A–F).
Escape Meaning
n New line
t Tab
b Backspace
r Carriage return
f Formfeed
Backslash
' Single quotation mark
" Double quotation mark
d Octal
xd Hexadecimal
ud Unicode character
Provided By Shipra Swati
5. INTRODUCTION TO JAVA PROGRAMMING LANGUAGE
4. String Literals: The set of characters in represented as String literals in Java. Always use
"double quotes" for String literals. A string in Java is an object rather than being a basic data
type, and strings are not stored in arrays as they are in languages such as C. There are few
methods provided in Java to combine strings, modify strings and to know whether to strings
have the same value.
Example:
String myString = "How are you?";
5. Boolean Literals: The values true and false are also treated as literals in Java programming.
When we assign a value to a boolean variable, we can only use these two values.
Example
boolean chosen = true;
Provided By Shipra Swati