0% found this document useful (0 votes)
2 views

Java

This document covers the fundamentals of Java programming, including identifiers, reserved words, data types, literals, and coding standards. It explains the rules for defining identifiers, the significance of reserved keywords, and the characteristics of various data types such as byte, short, int, long, float, double, boolean, and char. Additionally, it discusses how to use literals in different number systems and the enhancements introduced in Java 1.7 regarding literals.

Uploaded by

mohit mehra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java

This document covers the fundamentals of Java programming, including identifiers, reserved words, data types, literals, and coding standards. It explains the rules for defining identifiers, the significance of reserved keywords, and the characteristics of various data types such as byte, short, int, long, float, double, boolean, and char. Additionally, it discusses how to use literals in different number systems and the enhancements introduced in Java 1.7 regarding literals.

Uploaded by

mohit mehra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA

Unit 1: Language Fundamental: - Identifiers, Reserved Words, Data Types,


Literals, Arrays, Types of Variables, var – arg method, main method, Java
Coding Standard and Command Line Arguments
Identifiers: - A name is java program is called Identifier which can be used for
identification purpose it can be method name, variable name, class name
labelled name,

class Test {
public static void main (String[] args) {
int x = 10;
}
}

There are 5 Identifiers

Test = Class Name main = method name


String = predefined class Name args = variable name
x = name of array/variable

Rules for defining java identifiers:


1) The only allowed characters in java identifiers are a-z, A-Z, 0-9, $, _
if we are using any other character we will get compile time error.
Ex: total_number (valid) total# (invalid)
2) Identifiers can’t start with digit
Ex: total123 (valid) 123total (invalid)
3) Java identifier are Case Sensitive (java language can differentiate case) off
course Java language is treated as Case Sensitive programming Language.
Ex:
class Test
{
int number = 10; // valid
int Number = 20; // valid
int NUMBER = 30; // valid
}
4) There is no length Limit for java Identifier but it is not recommended to
take too Lengthy identifier
5) We can’t use reserved word as Identifiers
Ex: int x = 10 (Valid) int if = 10 invalid
6) All predefined java class name and interface name we can use as
Identifiers.
Ex:
class Test {
public static void main (String[] args) {
int Runnable = 880;
int String = 999;
System.out.println(Runnable); ANS: 880
System.out.println(String); 999
}
}
Even though it is valid but it is not a good programming practise because
it reduces readability and create confusion

Reserved word: - In java some words are reserved to represent some meaning
or functionality such type of words are called reserved words
Keywords vs Reserved Literal: Keywords are those reserved word which holds
some functionality but Reserved literals are those reserved word which do not
hold any functionality but are used as value.
Keyboards for Data types: -byte, short, int, long, float, double, char, boolean
Keyboards for flow control: - if, else, switch, case, default, break, continue,
return, for, while, do
Keyboards for modifiers: public, private, protected, static, final, synchronized,
strictfp, transient, volatile, native, abstract
Keyboards for Expectation Handling: try, catch, finally, throw, throws, assert
Class related Keyboards: class, interface, extends, import, package, implements
Object related Keyboards: new, instanceof, super, this
void Keyboard: In java return type is mandatory if a method won’t return
anything we have to declare method with void but in c return type is optional
and default return type is int
Unused Keyword: goto, const are unused keyboard and if e trying to use we
will get compile type error
goto: usage of goto create several problems in old language and hence Sun
people banned this keyboard in java
cost: use final instead of const
Reserved Literal: true, false, null true
true, false: These are boolean value
null: - default value object reference
*enum keyboard: we can use enum to define group of named constant
NOTE: In java we have only new keyboard and here is no delete keyboard
because destruction of useless object is the responsibility of garbage collector

Data Types:
In java Every Variable and Every Expression has some type. Each and Every
Data type is clearly defined in java. Every assignment should be checked by
compiler for type compatibility
(Because of above reason we can conclude java language is strongly typed
programming language)
NOTE: Java is not considered as pure OOP Language because several OOP’s
features are not satisfied by java (Like Operator Overloading, multiple
inheritance etc moreover we are depending on primitive Data types which are
non-objects)
Primitive Data Types:

Note: Except boolean and char remaining data type are considered as signed
Data types because we can represent both positive and negative numbers
byte: MSB LSB
# Size = 8 bits (1 byte)
Range = [-128 to 127] 7 6 5 4 3 2 1 0 (bits)
# The MSB bit (7th bit) is a signed bit means 0 represent as +ve number and 1
represented as -ve number +ve number will be represented directly in memory
but -ve number will be represented in 2’s complement form
Example:
byte b = 127 (Valid)
byte b = 130; (CE: possible lossy conversion from int to byte)
byte b = 10,78 (CE: possible lossy conversion from double to byte)
byte b = true (CE: incompatible types: boolean cannot be converted to byte)
USE: byte is the best choice if we won’t to handle data in terms of streams
either from the file or form the network (because file supported form or
network supported form is byte)
short: This is the most rarely used data type in java
# Size = 16bits (2 bytes)
Range = [−215 ¿215−1] / [-32768 to32767]
Example:
short s = 32767 (Valid)
short s = 32768; (CE: possible lossy conversion from int to short)
short s = 10,78 (CE: possible lossy conversion from double to short)
short s = true (CE: incompatible types: boolean cannot be converted to short)
USE: short data type is best suitable for 16 bits processor like 8085 but these
processors are completely outdated and hence corresponding short data type
is also outdated data type
int: the most commonly used data type in java is int
# Size = 32 bits (4 bytes)
Range = [−231 ¿ 231−1] / [-2147483648 to 2147483647]
Example
int i1 = 2147483647; (Valid)
int i2 = 2147483648; (CE: Integer number too large)
int i3 = 2147483648L; (CE: possible lossy conversion from long to int)
int i4 = true; (CE: incompatible types: boolean cannot be converted to int)
long:
Sometimes int may not enough to hold big values then we should go for long
type.
Example1: The amount of distance travelled by light in 1000 days, to hold this
value int may not enough we should go for long data type
long miles = 126000L*60*60*24*1000
Example2: The number of character present in big file may exceed int range
hence the return type of length () is long but not int
long l = f.length()
# Size: 8 bytes (64 bits)
Range = [−263 ¿2 63−1]
NOTE: All the above data types (byte short int long) meant for representing
integral values if we won’t to represent floating point values then we should go
for floating point data types.
Floating point data type
NOTE: If we want 5 to 6 decimal places of accuracy go for float and if we want
14 to 15 decimal place accuracy then go for double
float double
1) 5 to 6 decimal place accuracy 1) 14 to 15 decimal place accuracy
2) Less/Single precision 2) Mora/Double precision
3) Size = 4 bytes 3) Size = 8 bytes
4) Range = [-1.7e38 to 1.7e38] 4) Range = [-3.4e308 to 3,4e308]
boolean:
# Size: Not Applicable (Virtual machine dependent)
Range = Not Applicable (But allowed values are true or false)
Example:
boolean b = True; (CE: cannot find symbol
Symbol: variable True
Location: class Test)
char:
Old Languages like C/C++ are ASCII code based and the number of allowed
different character are less than are equal to 256 to represent these256
character 8 bits are enough hence the size of char in old languages is 1 byte
but java is UNICODE based and the number of different Unicode character are
>256 and <=65536. To represent these many character 8 bits may not enough
compulsory we should go for 16 bits hence size of char in java is 2 byte.
# Size = 2 bytes (16 bits)
Range = {0 to 65535]
NOTE: Null is the default value of Object reference and we can’t apply for
primitives if we are trying to use for primitive then we will get CE
Example :
char ch = null; (CE: incompatible types: <null> cannot be converted to char)

Literals: A constant value which can be assigned to the variable is called Literal
Example: int x = 10 (Literal/Constant value)
Integral Literal
For Integral data types we can specify literal value in the following ways
1) Decimal Form (Base = 10) :- Allowed Digits are 0 to 9 Example int x = 10;
2) Octal form (Base = 9 :- Allowed Digits are 0 to 7. The Literal value should be
prefixed with 0 Example int x = 010;
3) Hexadecimal Form (Base = 16) :- Allowed digits are 0 to 9 and a to f
(For Extra digit a to f we can use both lower case and uppercase characters this
is the very few areas where java is not Case Sensitive). The Literal Value should
be prefixed with 0x/0X Example: int x = 0X10;
Examples1:
int x = 10; (Valid)
int x = 0787 (CE: ; expected)
int x = 0777 (Valid)
int x = 0XBeef (Valid)
int x = 0xBeer (CE: ; expected)
# Programmer have the choice to specify the Integer value in octal, decimal,
Hexadecimal form but JVM will always provide the value in decimal form
Example
class practise {
public static void main(String[] args) {
int x = 10;
int y = 010;
int z = 0x10;
System.out.println(x+"_ _ _"+y+"_ _ _"+z);
}
}
ANS: 10_ _ _8_ _ _16

# By default, every integral literal is of Int type but we can specify explicitly as
long type by suffixed with l/L
# There is no direct way to specify byte and short literal explicitly but indirectly
we can specify whenever we are assigning integral literal to the byte variable
and if the value within the range of byte then compiler treat it automatically as
byte literal similarly short literal also
Example:
byte b = 127; (Valid)
byte b = 128; (CE: possible lossy conversion from int to byte)
Floating point Literals
# By default, every floating-point literal is of double type and hence we can’t
assign directly to the float variable but we can specify floating point literal as
float type by suffixed with f/F
Example:
float x= 123.567; (CE: possible lossy conversion from double to float)
float x = 123.567F; (Valid)
# We can specify explicitly floating-point literal as double type by suffixed with
d/D off course this convention is not required
# We can specify floating point literal only in decimal form and we can’t specify
in octal and hexadecimal forms
Example
double f1 = 123.456; (Valid)
double f2 = 0123.456; (Valid) (because it is decimal literal not octal literal)
double f3 = 0X123.456; (CE: malformed floating-point literal)
# We can assign Integral literal to floating point variable and that integral
literal can be specific either in decimal, octal or Hexadecimal forms
Example
double d = 10; (Valid)
double d = 010; (Valid)
double d = 0XFace; (Valid)
double d = 0XFace.0; (Invalid) (CE: malformed floating-point literal)
double d = 0777.0; (Valid)
# We can’t assign floating point literal to integral variable
# We can specify floating point literal even in exponential form (Scientific
notation)
Example:
double d = 1.2e4; (Valid) ANS: 12000.0
float d = 1,2e4; (CE: possible lossy conversion from float to double)
float d = 1,2e4F; (Valid) ANS: 12000.0
boolean Literal
The only allowed value for the boolean data type are true or false
Char Literal
# We can specify char literal as single character within single quotes.
Example:
char ch = ‘a’ (Valid)
char ch = ‘ab’ (CE: unclosed char literal
unclosed char literal
not a statement)
# We can specify char literal as integral literal which represents Unicode Value
of that character and that integral literal can be specified either is decimal,
octal and hexadecimal form but allowed range is 0 to 65535
Example
char ch = 0xFace; (Valid)
char ch = 0777 (Valid)
char ch = 65636 (CE: possible lossy conversion from int to char)
# We can represent char literal in Unicode representation which is nothing but
‘\u_ _ _ _’ (where _ _ _ _ 4 digit hexadecimal number (mandatory to write all
4 digits))
Example
char ch = '\u0061'; ANS: a
# Every escape character is a valid char literal
Example
char ch = ‘\n’; (Valid); char ch = ‘\m’;(CE: Invalid escape character)
Escape Character Functionality
Symbol
\n New line
\t Horizontal tab
\’ Single quote
\’’ Double quote
\\ Back Slash
\b Back Space
\f Form Feed
\r Carriage Return

String Literal: Any sequence of character within “ ” is treated as String literal


1.7v Enhancement w.r.to Literal
# For Integral Data Type Until i.6v we can specify literal values in Decimal,
Octal, Hexadecimal Form but from 1.7v onwards we can specify Literal value
even in binary from
Binary Form: Allowed digits are 0 and 1. Literal value should be prefixed with
0B/0b
Example:
int x = 0B1111 ANS = 15;
# Usage of _ (underscore) symbol: form 1.7v onward we can use underscore
symbol between digits of numeric literal. The main advantage of this approach
is readability of the code is improved.
Example
int x = 101101101 instead int x = 10_11_01_101 (Valid)
int x = 12357465.456 instead int x = 1_23_57_465.4_5_6 (Valid)
1) At the time of compile these underscore symbol will be removed
automatically
2) We can use more than one underscore symbol between the digits
double d = 1_ _ 23_561
(There should be no space between consecutive underscore)
3) We can use underscore symbol only between the digits if we are using
anywhere else we will get compile time error
Example
int x = _123 (Invalid)
NOTE:

8-byte long value we can assign to 4-byte float variable because both are
following different memory representation internally
float f = 10L (Valid)
2-byte short value cannot be assigned to 2-byte char variable and vice versa
because char is unsigned DT whereas short is signed data type

You might also like