Java Fundamentals

Java:
Java is a programming language, platform & technology.
Java is called as programming language because by using Java, we can write programs.

List of programming languages:

C, C++, Java, Python, VC++, Visual Basic.Net, C#.Net, Small talk, Simula, Ada, .. etc.,

Top 5 programming languages are Java, C, Python, C++ & C#

C & C++ are popular for developing system softwares
Java popular for developing Internet Applications
Python popular for developing Artificial Intelligence Applications
C# popular for developing Graphical User Interface Applications

Identifiers:

Identifier is a word and it is used to identify variable, method,
class, interface, package, .. etc.,
It can be a variable name, method name, class name, interface name, package name, .. etc.,

Rules to declare an identifier:

1) It can be formed by using alphabets(A to Z & a to z), digits (0 to 9), underscore symbol(_) and dollar symbol($).
2) It must begins with alphabet, underscore & dollar symbol.
3) The length of the identifier is not limited.
4) It should not contain special symbols other than underscore symbol or dollar symbol.
5) It should not contain white space characters(Space bar, tab & enter keys).

Keywords:

A set of words reserved by language itself and those words are called keywords.
There are 50 keywords at present in Java language including strictfp, assert & enum.
All keywords must be written in lowercase letters only.

strictfp keyword introduced in JDK 1.2 version in 1998.

assert keyword introduced in JDK 1.4 version in 2002.
enum keyword introduced in JDK 1.5 version in 2004.

Note1: const & goto keywords presently not in use.

Note2: Keyword cannot be used as an identifier.

Literals:

A literal is a source code representation of a fixed value.
In Java, literals are divided into 6 categories:

1) Integer Literals:

5, 92, 4342, 0, -3, -45, -343, .. etc.,
2) Floating Point Literals:
5.2, 432.09, 0.093, -2.334, .. etc.,
3) Character Literals:
'a', 'b', 'z', 'X', .. etc.,
4) String Literals:
"Hi", "hello", "Welcome", .. etc.,
5) Boolean Literals:
true, false
6) Object Literals:
null

Note1: true, false & null are not keywords.

Note2: true,false & null are also cannot be used as an identifier.

Data Types:

A date type that determines what value variable can hold and what are the operations can be performed on variables.

In Java, data types are divided into 2 categories:

1) Primitive Data Types:
2) Reference Data Types:

Primitive Data Types:

All primitive data types are predefined data types. There are 8 primitive data types and these are named by keywords.
1) byte 2) short 3) int 4) long 5) float 6) double 7) char 8) boolean

Reference Data Types:

Arrays, Strings, Classes, Interfaces, .. etc.,

Type Conversions:

boolean type cannot be converted to other types and other types cannot be converted to boolean type.

The following 19 conversions are done by system implicitly:

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:
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   
                                                     ================



Assignment1:
If we compile and run the following code snippets, what will be happen?


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. 




Assignment2:
If we compile and run the following code snippets, what will be happen?



Java Library:

Java library organised in the form of packages. A package is a collection of sub packages, classes & interfaces.
java.lang package is a default package and it is implicitly imported in every java program.


Java Notations:
1) Package & Sub Package: All small letters and sub packages are separated with dot(.) symbol.
2) Class & Interface: Each word first letter is capital and no space between words.
3) Variable & Method: Second word onwards first letter is capital and no space between words.



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 and public class name must be same.
4) If the source file does not contain public class then no naming restrictions to a file name.



Assignment3: