Language fundamentals
---------------------
1)variables
2)keywords
3)constants
4)datatypes
5)operators
variable
--------
*Variable is an identifier which identifies a value,whose value changes.
syntax
------
datatype variablename;
ex:
int a;
Rules for declaring variables
-----------------------------
*A variable should start with alphabet or underscore or dollar ,after that we can use numbers in a variable.
*No special symbols are allowed in a variable except underscore(_) and dollar($)
*No spaces are allowed in a variable.
ex:
a(v)
b(v)
abc123(v)
123abc(IV)
a_bc_c(v)
a+b+c(iv)
a bc(iv)
s_(v)
_$(v)
$123(v)
%@abc(iv)
$(v)
_(v)
abc 123(iv)
Keywords
--------
*keywords are builtin or predefined words.
*There are around 53 keywords in java
ex:
int
char
float
double
long
while
do
for
class
etc
Rules for declaring keywords
----------------------------
i)A keyword must be declared in lowercase.
ii)we cannot use keywords as identifiers.
ex:
INT(ic)
Float(ic)
while(c)
int int;(iv)
int x;(c)
Constants
---------
Constant is a fixed value.
There are 3 types of constants.
i)character
ii)Numeric
iii)boolean
character constants
-------------------
character constants are
i)words.
ii)alphabets[a-z,A-Z].
iii)digits[0-9].
iv)special symbols.
character constants are of 2 types.
i)single character constants
ii)String constants.
i)single chracter constant
--------------------------
single character enclosed in single quotes is known as
single character constant.
ex:
'a'
'x'
'5'
'*'
''
'10'(ic)
'abc'(ic)
String constants
----------------
group of letters enclosed in double quotes is known as
string constant.
ex:
"satya"
"satya123"
""
" "
"a"
"12345"
Numeric constants
-----------------
All numbers are numeric constants
They are 2 types
Integer constant
decimal/real/floating-point constant.
Integer constant
----------------
Number without fractional part is called integer constant.
ex:
10
20
56
decimal constant.
-----------------
Number with fractional part is called decimal constant.
ex:10.7
5.6
we can represent integer constant in the following number systems.
i)Decimal(regular)
ii)binary
iii)octal
iv)hexa decimal system
decimal number system
---------------------
It has base as 10 i,e it consists base numbers from 0 to 9
ex:19
20
1
158
Binary number system
--------------------
It has base as 2 i,e it consists of digits 0 or 1.
ex:010
110
etc
converting decimal to binary
----------------------------
if we want to convert decimal number to binary we should perform lcm using 2.
536 --> ?
10 2
2|536
---
2|268-0
---
2|134-0
---
2|67-0
--
2|33-1
----
2|16-1
----
2|8-0
---
2|4-0
--
2|2-0
----
2|1-0
---
2|0-1
collect remainders from bottom to top
536-->1000011000
converting binary to decimal
----------------------------
1 0 0 0 0 1 1 0 0 0
2^9 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
2^9*1+2^8*0+..........+2^0*0.
512+0+.......+0
536
octal number system
-------------------
It has a base as 8 i'e it has digits from 0 to 7.
Always an octal integer must have a prefix 0[zero].
ex:
0123(v)
012(v)
123(iv)
078(iv)
converting decimal to octal
---------------------------
36--> ?
10 8
8|36
--
8|4-4
---
8|0-4
octal-->044
converting octal to decimal
---------------------------
044--> ?
10
0 4 4
8^2 8^1 8^0
0+32+4-->36
Hexa decimal system
-------------------
It has base as 16 i'e it consists altogether digits from 0 to 15
0 to 9[digits]
from 10-15 we should substitute alphabets a-f
10->a or A
11->b or B
12->c or C
13->d or D
14->e or E
15->f or F
It should have a prefix 0x or 0X
ex:
0x123
0xabc
0xface
etc
converting decimal to hexa decimal
16|90
--
16|5-10
----
16|0-5
ans->0x5A
How many number systems?
decimal has a base-----
binary has a bas-----
octal has a base-----
hexa decimal has a base----
ocatl has a prefix 0x.
hexa has a prefix 0x
binary has a prefix-----
Integer constants
By default integer constant is of----type
Boolean constants
-----------------
If a condition is evaluated it may either return true or false based on input by user
here the boolean constants are true/false
we must declare boolean constants in lowercase because both are predefined literals.
datatype
--------
datatype is keyword which tells about type of data we are using in a program.
datatype also allocates memory for a variable.
syntax
------
datatype varname;
or
datatype var1,var2,....;
ex:
char cha;
variable declaration and initialization
---------------------------------------
we can declare and initialize a variable as shown below,
type1
-----
datatype var=value;
ex:
char ch='4';
type2
-----
datatype varname;
varname=value;
ex:
char ch;
ch='4';
declare and initialize character variable
char x='a';
declare and initialize byte variable
byte x=10;
declare and initialize short variable
short x=20;
declare and initialize int variable
int x=40;
declare and initialize long variable
long x=50;
declare and initialize float variable
float x=10.5f;
declare and initialize double variable
double x=10.5;
declare and initialize boolean variable
boolean x=true;
declare and initialize String variable
String x="manohar";
what is a variable?
variable syntax.
variable rules
A variable should start with ---/---/---
variables should not have ----- and -----
keywords must declared in -------.
keywords must not be declared as -------
what is datatype?
How many datatypes?
datatype types?
----is a reference type.
decimal literals
-----------------
*by default a decimal literal is of double type
*we should suffix a decimal literal with f/F while initializing it with a float variable.
*Java is very strict with type conversion,(however we can convert small datatype to big datatype but reverse is not possible automatically.)
* we cannot declare decimal literals as ocatal/hexadecimal ,vialation leads to compilation error.
*we can suffix a decimal literal with d/D while initializing to a double variable but it is optional.
ex:
float f=5.6;
double d=5.6;
double d=5.6D;
float f=10.5f;
double d=0123.456;
double d=0X123.456;
double d=0123;
double d=10;
float f=345;
Note:
we can suffix a long explicitly with l/L.
long l=10L;
long l=10l;
character literals
------------------
single letter enclosed in single quotes is known as single character constants.
we can represent single character constants in 2 ways in java
i)regular
ii)unicode
regular
-------
ex:
'a'
'5'
etc
unicode
-------
It is a 4 digit hexa decimal representation of a character.
syntax
------
'\uxxxx'
ex:
'\u0065'
'\u00be'
'\uface'
'\Ubeer'
*The only unsigned datatype in java is char
*unsigned datatype accpets only positive values.
*singned datatype accepts both negative and positive values.
which of the following declarations are valid?
Note:
we can also initialize integer lietrals to char variable.
Program structure of java
-------------------------
package declaration;
import statements;
class ClassName
{
public static void main(String args[]){
....;
....;
}
}
How to save a java program
--------------------------
ClassName.java/filename.java
How to compile a java program
-----------------------------
javac ClassName.java/filename.java
How to execute a java program
-----------------------------
java ClassName/filename
Naming conventions in java
--------------------------
A ClassName/interfacename/enumNam, every word starting letter must be uppercase
ex:
CarTest
SoftwareEngineer
A variable/methodname must start with lowercase and every inner word starting letter must be uppercase.
ex:
nextLine()
empName
A keyword/package must be declared in lowercase
ex:
int
while
pack1
etc
Installing jdk
--------------
we can download jdk from "oracle.com"
when we install a jdk we get 2 folders
1)jdk
2)jre
Jdk[java development kit]
-------------------------
It consists of development tools[java,javac,javah,javap etc] and jre
jdk=development tools+jre
Jre[java runtime environment]
-----------------------------
It consists of jvm and runtime libraries
jre=jvm+runtime libraries.
once jdk installation set classpath and path
path:It is system environment variable used to locate executable files from local file system.
How to set path
---------------
Goto-->c:/-->program files-->java-->jdk-->bin
copy the path from the address bar
C:\Program Files\Java\jdk1.8.0_111\bin
Now goto mycomputer-->right click-->properties-->advanced system settings->environment variables-->Under user variables
click on New-->variablename--path
variable value--
C:\Program Files\Java\jdk1.8.0_111\bin
classpath:It is a java environment variable which is used to locate .class files of java.
How to set classpath
---------------
Goto-->c:/-->program files-->java-->jdk-->bin
copy the path from the address bar
C:\Program Files\Java\jre1.8.0_111\lib\rt.jar
Now goto mycomputer-->right click-->properties-->advanced system settings->environment variables-->Under user variables
click on New-->variablename--classpath
variable value--
C:\Program Files\Java\jre1.8.0_111\lib\rt.jar
Note:open command prompt-->type the commands
c:/>javac
Here we should find some summary
c:/>java
Here also we should find some summary.
Operator
--------
An operator is a symbol which performs an operation
1)Arithmetic operators
2)relational operators
3)logical operators
4)Assignment operators
5)unary operators
6)ternary operators
7)bitwise operators
8)concatenation operators
9)[]-index operator
9)instanceof
10)new
11).(member access operator)
Arithmetic
-----------
+
-
*
%--remainder
/--quotient
ex:
int a=10,b=5,c;
c=a%b;-->0(remainder)
c=a/b;-->2(quotient)
relational
----------
compares values
>--greater than
<--less than
>=--greater than or equal
<=--less than or equal
==--equal
!=--not equal
ex:
int a=10,b=20;
a>b--false
Note:Any relational operation returns a boolean value i,e either true/false.
Logical operators
-----------------
These are used to combine 2 or more statements
They are
i)logical and--&&
ii)logical or--||
iii)logical not--!
logical and--&&
---------------
It is used to combine 2 or more conditions but the expression returns true if every condition is true otherwise false.
ex:
int s1=40,s2=50
s1>=40&&s2>=40-->true
logical or--||
---------------
It is used to combine 2 or more conditions but the expression returns true if any one condition is true or every condition is true,otherwise if all the condition's all false then or is false.
ex:
int s1=40,s2=30
s1>=40||s2>=40-->true
ex:
true&&true-->true
true&&false-->false
true||true-->true
false||false-->false
false&&false-->false
false&&true-->false
false||true-->true
true||false-->true
true&&true&&false&&true||true-->true
logical not(!)
--------------
It is used as a negation
syntax
------
!(expression)
ex:
!(true)-->false
int a=10,b=7;
!(a>b) -->false
Assignment operator
-------------------
An assignment operator assigns a value to a variable.
syntax
------
datatype varname=value;
ex:
int a=10;
unary operators
---------------
An expression is collection operands and operators
ex:
a+b+c
operand
-------
An operand can be a variable or an expression.
Def:
An operator applied on a single operand is known as unary operator.
we have 2 unary operators
i)incrementation(++)
ii)decrementation(--)
incrementation(++)
------------------
++ increments the value of an operand by 1
syntax
------
(expression)++;
ex:
int a=10;
Sop(a);//10
a++;
Sop(a);//11
Forms of incrementation
-----------------------
a++-->post inc
++a-->pre inc
a=a+1-->expanded form
a+=1-->short hand
post inc:
In this first initialization is done and then incremented.
ex:
int a=10;
b=a++;
a=11 b=10
Here first the value of a is assigned/initialized to b and then a gets incremented.
pre inc:
In this first incrementation is done and then initialized.
ex:
int a=10;
b=++a;
a=11 b=11
Here first the value of a is incremented and then initialized to b.
what is the ouput?
int a=6;
sop(a++);//
sop(a);//
case 5:
difference between b++ and b=b+1;
ex1:
byte b=10;
b++;
Sop(b);//11
byte b=10;
b=b+1;
Sop(b);//ce
explanation
-----------
In java when ever we perform any operation all the operands in the expression(logic) must be of same type,otherwise compiler will perform upcasting(small datatype to big datatype) for required operands.In this process a method is called to apply possible promotions i,e
max(int,typeofvar1,typeofvar2);
ex:
byte b=10;
b=b+1;
b is of type byte
1 is of type int(default int type)
max(int,byte,int)-->int
Here this method returns int,so compiler here promotes byte to int
int+int-->becomes int-->
final result is returned in int,but the operand at left side is byte so both are not compatible,we get compilation error.
Possible conversions in java
----------------------------
byte+byte-->int[max(int,byte,byte)]
byte+short-->int
short+short-->int
byte+char-->int
short+int-->int
int+int-->int
int+long-->long
int+float-->float
int+double-->double
float+long-->float
float+double-->double
long+double-->double
Ternary operators
-----------------
Operators applied on 3 operands is known as ternary operators.
Used to validate a condition.
syntax
------
expression1?expression2:expression3;
if expression1 is true then the statement after question mark(?) is executed otherwise the statement after colon(:) is executed.
ex:
int a=50,b=20,c;
c=a>b?a:b;
System.out.println(c);//50
Bitwise operators
-----------------
Bitwise operators are used perform operation on bits.
1)Bitwise and-->&
2)Bitwise or-->|
3)Bitwise xor-->^
4)Bitwise right shift-->>
5)Bitwise unsigned right shift-->>>
6)Bitwise left shift--><<
7)Bitwise compliment-->~
Type conversion/type casting
----------------------------
The process of converting one datatype to another datatype is is known as type conversion.
There are 2 types of conversions
i)Implicit conversion.
ii)explicit conversion.
Implicit conversion
-------------------
Converting one datatype to another datatype automatically by a compiler is known as Implicit conversion.
ex:
class Test{
public static void main(String args[])
{
byte b=10;
int a=b;//line1
System.out.println(a);
}
}
output:10
exp
---
In the above code at line1 compiler promotes byte to int automatically and initializes the variable a.
Implicit conversion is also called as widening or upcasting.
Explicit conversion
-------------------
Converting one datatype to another datatype programatically is known as explicit conversion.
syntax
------
datatype var1=(type)var2;
ex:
class Test{
public static void main(String args[])
{
int s=10;
byte a=(byte)s;//line1
System.out.println(a);
}
}
Exp:
---
In the above program at line1 we are explicitly promoting int to byte by using casting.
Explicit conversion is also called as narrowing or downcasting.
---------------------
1)variables
2)keywords
3)constants
4)datatypes
5)operators
variable
--------
*Variable is an identifier which identifies a value,whose value changes.
syntax
------
datatype variablename;
ex:
int a;
Rules for declaring variables
-----------------------------
*A variable should start with alphabet or underscore or dollar ,after that we can use numbers in a variable.
*No special symbols are allowed in a variable except underscore(_) and dollar($)
*No spaces are allowed in a variable.
ex:
a(v)
b(v)
abc123(v)
123abc(IV)
a_bc_c(v)
a+b+c(iv)
a bc(iv)
s_(v)
_$(v)
$123(v)
%@abc(iv)
$(v)
_(v)
abc 123(iv)
Keywords
--------
*keywords are builtin or predefined words.
*There are around 53 keywords in java
ex:
int
char
float
double
long
while
do
for
class
etc
Rules for declaring keywords
----------------------------
i)A keyword must be declared in lowercase.
ii)we cannot use keywords as identifiers.
ex:
INT(ic)
Float(ic)
while(c)
int int;(iv)
int x;(c)
Constants
---------
Constant is a fixed value.
There are 3 types of constants.
i)character
ii)Numeric
iii)boolean
character constants
-------------------
character constants are
i)words.
ii)alphabets[a-z,A-Z].
iii)digits[0-9].
iv)special symbols.
character constants are of 2 types.
i)single character constants
ii)String constants.
i)single chracter constant
--------------------------
single character enclosed in single quotes is known as
single character constant.
ex:
'a'
'x'
'5'
'*'
''
'10'(ic)
'abc'(ic)
String constants
----------------
group of letters enclosed in double quotes is known as
string constant.
ex:
"satya"
"satya123"
""
" "
"a"
"12345"
Numeric constants
-----------------
All numbers are numeric constants
They are 2 types
Integer constant
decimal/real/floating-point constant.
Integer constant
----------------
Number without fractional part is called integer constant.
ex:
10
20
56
decimal constant.
-----------------
Number with fractional part is called decimal constant.
ex:10.7
5.6
we can represent integer constant in the following number systems.
i)Decimal(regular)
ii)binary
iii)octal
iv)hexa decimal system
decimal number system
---------------------
It has base as 10 i,e it consists base numbers from 0 to 9
ex:19
20
1
158
Binary number system
--------------------
It has base as 2 i,e it consists of digits 0 or 1.
ex:010
110
etc
converting decimal to binary
----------------------------
if we want to convert decimal number to binary we should perform lcm using 2.
536 --> ?
10 2
2|536
---
2|268-0
---
2|134-0
---
2|67-0
--
2|33-1
----
2|16-1
----
2|8-0
---
2|4-0
--
2|2-0
----
2|1-0
---
2|0-1
collect remainders from bottom to top
536-->1000011000
converting binary to decimal
----------------------------
1 0 0 0 0 1 1 0 0 0
2^9 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
2^9*1+2^8*0+..........+2^0*0.
512+0+.......+0
536
octal number system
-------------------
It has a base as 8 i'e it has digits from 0 to 7.
Always an octal integer must have a prefix 0[zero].
ex:
0123(v)
012(v)
123(iv)
078(iv)
converting decimal to octal
---------------------------
36--> ?
10 8
8|36
--
8|4-4
---
8|0-4
octal-->044
converting octal to decimal
---------------------------
044--> ?
10
0 4 4
8^2 8^1 8^0
0+32+4-->36
Hexa decimal system
-------------------
It has base as 16 i'e it consists altogether digits from 0 to 15
0 to 9[digits]
from 10-15 we should substitute alphabets a-f
10->a or A
11->b or B
12->c or C
13->d or D
14->e or E
15->f or F
It should have a prefix 0x or 0X
ex:
0x123
0xabc
0xface
etc
converting decimal to hexa decimal
16|90
--
16|5-10
----
16|0-5
ans->0x5A
How many number systems?
decimal has a base-----
binary has a bas-----
octal has a base-----
hexa decimal has a base----
ocatl has a prefix 0x.
hexa has a prefix 0x
binary has a prefix-----
Integer constants
By default integer constant is of----type
Boolean constants
-----------------
If a condition is evaluated it may either return true or false based on input by user
here the boolean constants are true/false
we must declare boolean constants in lowercase because both are predefined literals.
datatype
--------
datatype is keyword which tells about type of data we are using in a program.
datatype also allocates memory for a variable.
syntax
------
datatype varname;
or
datatype var1,var2,....;
ex:
char cha;
variable declaration and initialization
---------------------------------------
we can declare and initialize a variable as shown below,
type1
-----
datatype var=value;
ex:
char ch='4';
type2
-----
datatype varname;
varname=value;
ex:
char ch;
ch='4';
declare and initialize character variable
char x='a';
declare and initialize byte variable
byte x=10;
declare and initialize short variable
short x=20;
declare and initialize int variable
int x=40;
declare and initialize long variable
long x=50;
declare and initialize float variable
float x=10.5f;
declare and initialize double variable
double x=10.5;
declare and initialize boolean variable
boolean x=true;
declare and initialize String variable
String x="manohar";
what is a variable?
variable syntax.
variable rules
A variable should start with ---/---/---
variables should not have ----- and -----
keywords must declared in -------.
keywords must not be declared as -------
what is datatype?
How many datatypes?
datatype types?
----is a reference type.
decimal literals
-----------------
*by default a decimal literal is of double type
*we should suffix a decimal literal with f/F while initializing it with a float variable.
*Java is very strict with type conversion,(however we can convert small datatype to big datatype but reverse is not possible automatically.)
* we cannot declare decimal literals as ocatal/hexadecimal ,vialation leads to compilation error.
*we can suffix a decimal literal with d/D while initializing to a double variable but it is optional.
ex:
float f=5.6;
double d=5.6;
double d=5.6D;
float f=10.5f;
double d=0123.456;
double d=0X123.456;
double d=0123;
double d=10;
float f=345;
Note:
we can suffix a long explicitly with l/L.
long l=10L;
long l=10l;
character literals
------------------
single letter enclosed in single quotes is known as single character constants.
we can represent single character constants in 2 ways in java
i)regular
ii)unicode
regular
-------
ex:
'a'
'5'
etc
unicode
-------
It is a 4 digit hexa decimal representation of a character.
syntax
------
'\uxxxx'
ex:
'\u0065'
'\u00be'
'\uface'
'\Ubeer'
*The only unsigned datatype in java is char
*unsigned datatype accpets only positive values.
*singned datatype accepts both negative and positive values.
which of the following declarations are valid?
Note:
we can also initialize integer lietrals to char variable.
Program structure of java
-------------------------
package declaration;
import statements;
class ClassName
{
public static void main(String args[]){
....;
....;
}
}
How to save a java program
--------------------------
ClassName.java/filename.java
How to compile a java program
-----------------------------
javac ClassName.java/filename.java
How to execute a java program
-----------------------------
java ClassName/filename
Naming conventions in java
--------------------------
A ClassName/interfacename/enumNam, every word starting letter must be uppercase
ex:
CarTest
SoftwareEngineer
A variable/methodname must start with lowercase and every inner word starting letter must be uppercase.
ex:
nextLine()
empName
A keyword/package must be declared in lowercase
ex:
int
while
pack1
etc
Installing jdk
--------------
we can download jdk from "oracle.com"
when we install a jdk we get 2 folders
1)jdk
2)jre
Jdk[java development kit]
-------------------------
It consists of development tools[java,javac,javah,javap etc] and jre
jdk=development tools+jre
Jre[java runtime environment]
-----------------------------
It consists of jvm and runtime libraries
jre=jvm+runtime libraries.
once jdk installation set classpath and path
path:It is system environment variable used to locate executable files from local file system.
How to set path
---------------
Goto-->c:/-->program files-->java-->jdk-->bin
copy the path from the address bar
C:\Program Files\Java\jdk1.8.0_111\bin
Now goto mycomputer-->right click-->properties-->advanced system settings->environment variables-->Under user variables
click on New-->variablename--path
variable value--
C:\Program Files\Java\jdk1.8.0_111\bin
classpath:It is a java environment variable which is used to locate .class files of java.
How to set classpath
---------------
Goto-->c:/-->program files-->java-->jdk-->bin
copy the path from the address bar
C:\Program Files\Java\jre1.8.0_111\lib\rt.jar
Now goto mycomputer-->right click-->properties-->advanced system settings->environment variables-->Under user variables
click on New-->variablename--classpath
variable value--
C:\Program Files\Java\jre1.8.0_111\lib\rt.jar
Note:open command prompt-->type the commands
c:/>javac
Here we should find some summary
c:/>java
Here also we should find some summary.
Operator
--------
An operator is a symbol which performs an operation
1)Arithmetic operators
2)relational operators
3)logical operators
4)Assignment operators
5)unary operators
6)ternary operators
7)bitwise operators
8)concatenation operators
9)[]-index operator
9)instanceof
10)new
11).(member access operator)
Arithmetic
-----------
+
-
*
%--remainder
/--quotient
ex:
int a=10,b=5,c;
c=a%b;-->0(remainder)
c=a/b;-->2(quotient)
relational
----------
compares values
>--greater than
<--less than
>=--greater than or equal
<=--less than or equal
==--equal
!=--not equal
ex:
int a=10,b=20;
a>b--false
Note:Any relational operation returns a boolean value i,e either true/false.
Logical operators
-----------------
These are used to combine 2 or more statements
They are
i)logical and--&&
ii)logical or--||
iii)logical not--!
logical and--&&
---------------
It is used to combine 2 or more conditions but the expression returns true if every condition is true otherwise false.
ex:
int s1=40,s2=50
s1>=40&&s2>=40-->true
logical or--||
---------------
It is used to combine 2 or more conditions but the expression returns true if any one condition is true or every condition is true,otherwise if all the condition's all false then or is false.
ex:
int s1=40,s2=30
s1>=40||s2>=40-->true
ex:
true&&true-->true
true&&false-->false
true||true-->true
false||false-->false
false&&false-->false
false&&true-->false
false||true-->true
true||false-->true
true&&true&&false&&true||true-->true
logical not(!)
--------------
It is used as a negation
syntax
------
!(expression)
ex:
!(true)-->false
int a=10,b=7;
!(a>b) -->false
Assignment operator
-------------------
An assignment operator assigns a value to a variable.
syntax
------
datatype varname=value;
ex:
int a=10;
unary operators
---------------
An expression is collection operands and operators
ex:
a+b+c
operand
-------
An operand can be a variable or an expression.
Def:
An operator applied on a single operand is known as unary operator.
we have 2 unary operators
i)incrementation(++)
ii)decrementation(--)
incrementation(++)
------------------
++ increments the value of an operand by 1
syntax
------
(expression)++;
ex:
int a=10;
Sop(a);//10
a++;
Sop(a);//11
Forms of incrementation
-----------------------
a++-->post inc
++a-->pre inc
a=a+1-->expanded form
a+=1-->short hand
post inc:
In this first initialization is done and then incremented.
ex:
int a=10;
b=a++;
a=11 b=10
Here first the value of a is assigned/initialized to b and then a gets incremented.
pre inc:
In this first incrementation is done and then initialized.
ex:
int a=10;
b=++a;
a=11 b=11
Here first the value of a is incremented and then initialized to b.
what is the ouput?
int a=6;
sop(a++);//
sop(a);//
case 5:
difference between b++ and b=b+1;
ex1:
byte b=10;
b++;
Sop(b);//11
byte b=10;
b=b+1;
Sop(b);//ce
explanation
-----------
In java when ever we perform any operation all the operands in the expression(logic) must be of same type,otherwise compiler will perform upcasting(small datatype to big datatype) for required operands.In this process a method is called to apply possible promotions i,e
max(int,typeofvar1,typeofvar2);
ex:
byte b=10;
b=b+1;
b is of type byte
1 is of type int(default int type)
max(int,byte,int)-->int
Here this method returns int,so compiler here promotes byte to int
int+int-->becomes int-->
final result is returned in int,but the operand at left side is byte so both are not compatible,we get compilation error.
Possible conversions in java
----------------------------
byte+byte-->int[max(int,byte,byte)]
byte+short-->int
short+short-->int
byte+char-->int
short+int-->int
int+int-->int
int+long-->long
int+float-->float
int+double-->double
float+long-->float
float+double-->double
long+double-->double
Ternary operators
-----------------
Operators applied on 3 operands is known as ternary operators.
Used to validate a condition.
syntax
------
expression1?expression2:expression3;
if expression1 is true then the statement after question mark(?) is executed otherwise the statement after colon(:) is executed.
ex:
int a=50,b=20,c;
c=a>b?a:b;
System.out.println(c);//50
Bitwise operators
-----------------
Bitwise operators are used perform operation on bits.
1)Bitwise and-->&
2)Bitwise or-->|
3)Bitwise xor-->^
4)Bitwise right shift-->>
5)Bitwise unsigned right shift-->>>
6)Bitwise left shift--><<
7)Bitwise compliment-->~
Type conversion/type casting
----------------------------
The process of converting one datatype to another datatype is is known as type conversion.
There are 2 types of conversions
i)Implicit conversion.
ii)explicit conversion.
Implicit conversion
-------------------
Converting one datatype to another datatype automatically by a compiler is known as Implicit conversion.
ex:
class Test{
public static void main(String args[])
{
byte b=10;
int a=b;//line1
System.out.println(a);
}
}
output:10
exp
---
In the above code at line1 compiler promotes byte to int automatically and initializes the variable a.
Implicit conversion is also called as widening or upcasting.
Explicit conversion
-------------------
Converting one datatype to another datatype programatically is known as explicit conversion.
syntax
------
datatype var1=(type)var2;
ex:
class Test{
public static void main(String args[])
{
int s=10;
byte a=(byte)s;//line1
System.out.println(a);
}
}
Exp:
---
In the above program at line1 we are explicitly promoting int to byte by using casting.
Explicit conversion is also called as narrowing or downcasting.
No comments:
Post a Comment