Java Fundamentals part-2
Type Conversions:
boolean type cannot be converted to
other types & other types cannot be converted to boolean type.
The following 19 conversions are done
by system implicitly. These conversions are called widening conversions:
byte
to short, int, long, float, double =>
5
short
to int, long, float, double =>
4
int
to long, float, double =>
3
long
to float, double => 2
float
to double => 1
char
to int, long, float, double =>
4
========================
Total =>19
========================
The following 23 conversions are must
be done by programmer explicitly otherwise compile time error occurs. These
conversions are called narrowing conversions.
byte
to char =>
1
short
to byte, char =>
2
int
to byte, short, char =>
3
long
to byte, short, int, char =>
4
float
to byte, short, int, long, char =>
5
double
to byte, short, int, long, float, char =>
6
char
to byte, short =>
2
=============================
Total =>23
=============================
Example1:
class Demo
{
public
static void main(String args[])
{
char
a=’A’;
int
b=a;
System.out.println(b);
}
}
Example2:
class Demo
{
public
static void main(String args[])
{
int
a=65;
char
b=a;
System.out.println(b);
}
}
Example3:
class Demo
{
public
static void main(String args[])
{
int
a=65;
char
b=(char)a;
System.out.println(b);
}
}
Example4:
class Demo
{
public
static void main(String args[])
{
boolean
a=true;
char
b=(boolean)a;
System.out.println(b);
}
}
Operations on data:
1) (byte, short, int, char) + (byte,
short, int, char) => int
2) (byte, short, int, long, char) + long => long
3) (byte, short, int, long, float, char) + float => float
4) (byte, short, int, long, float,
double, char) + double => double
Note:
Operations cannot be performed on boolean types.
Example1:
class Demo
{
public
static void main(String args[])
{
byte
a=5;
byte
b=3;
byte
c=a+b;
System.out.println(c);
}
}
Example2:
class Demo
{
public
static void main(String args[])
{
int
a=5;
char
b=’A’;
float
c=a+b;
System.out.println(c);
}
}
Example3:
class Demo
{
public
static void main(String args[])
{
int
a=5;
char
b=’a’;
byte
c=(byte)(a+b);
System.out.println(c);
}
}
Example4:
class Demo
{
public
static void main(String args[])
{
int
a=5;
char
b=’A’;
char
c=(char)a+b;
System.out.println(b);
}
}
Declaration rules to a source file(.java file):
1) A source file can have only one public class.
2) A source file can have any number of non public classes.
3) If the source file contains public class then file name must match with public class name.
4) If the source file does not contain
public class then no naming restrictions to a public class name.
Java Program Structure:
class Demo
{
public static void main(String args[])
{
System.out.println(“Welcome
to Java”);
}
}
In the above
example String & System classes are the part of java.lang package.
java.lang
package is a default package and it is implicitly imported in every java
program.
Compilation:
C:\>javac
Demo.java
The Java
compiler generates .class file for every class in a source file.
Execution:
C:\>java
Demo
A class that
contains main() method only can be used to execute the program.
javac&
java are called jdk tools. All jdk tools are the part of bin folder in JDK.
JDK stands
for Java Development Kit. It is called as Java Software. Latest version of JDK
is 21.
Download & Install JDK:
To download
JDK, type the following url in address bar in a browser window.
https://www.oracle.com/in/java/technologies/javase-downloads.html
After
downloading double click on downloaded file to install.
By
Mr. Venkatesh Mansani
No comments:
Post a Comment