Introduction to Java
Java is a class based object oriented programming language
developed by James Gosling and his colleagues at Sun
Microsystems in 1991 and later acquired by Oracle
Corporation. The language was initially called Oak(named
after the Oak tree outside gosling office).
Basic features of Java
Java is an object oriented programming language.
Java programs are both compiled as well as interpreted.
Java program is written within a class.
Java is a case sensitive. The uppercase and lowercase letters are
distinguished by the language.
Different types of Java Programs
a. Java Applications
b. Java Applet
Java Standalone Application is an application that can run
locally on the device and does not require anything else to be
functional.
Applets are the special kind of Java programs that run on
Java enabled browsers.Basically, they are small java
applications that can be accessed on the internet server,
transported over the internet and run automatically as part
of a web document.
Comments
The comment statement is a non-executable statement in
Java. It included for the user to understand what action is
going to be taken in a program or a statement.
Tokens
All characters in a java program are grouped into symbols
called Tokens. A computer program is a set of instructions
called statements.A statement is composed of various
components.Each individual component of a programming
statement is referred to as tokens.
There are five categories of token in Java.These are:
1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators
Keywords
Keywords,also known as Reserved Words,are the words
that have a special meaning to the Java compiler. Java
compiler reserves these keywords for its own use and
hence they are not available as names for variables or
methods.
Identifiers
Identifiers are used to name different components of a program
such as variables, methods, objects etc.
Java identifier naming conventions
An identifier can consists of any combinations of letters, digits ,
the underscore character (_) and the dollar sign9$).
An identifier cannot begin with a digit.
Identifiers may be of any length.
Both uppercase and lowercase letters can be used in naming
identifier.
Java is a case sensitive, which means that two identifier names
that differ only in uppercase and lowercase characters are
considered to be different identifiers. Therefore Joseph, JOseph,
joseph, JOSEPH are all different identifiers.
Literals
Literals are fixed values that are directly written into the
code. They represent constant values of a specific data type
and are used to assign values to variables or for use in
expressions.
Types of Literals in Java:
Integer Literals – Whole numbers without decimal points
Example: 10, -5, 0
Floating-Point Literals – Numbers with decimal points
Example: 3.14, -0.001, 2.5e3
By default, a floating-point literal is of type double. Use f
or F to indicate a float.
Character Literals – A single character enclosed in single
quotes
Example: 'A', '9', '\n'
String Literals – A sequence of characters enclosed in
double quotes
Example: "Hello", "Java123"
Boolean Literals – Only two possible values:
true or false
Null Literal – Represents the null reference
Only one literal: null
Examples:
int age = 25; // Integer literal
double pi = 3.14159; // Floating-point literal
char grade = 'A'; // Character literal
String name = "Alice"; // String literal
boolean isJavaFun = true; // Boolean literal
Tokens are the smallest elements of a program that are
meaningful to the compiler. The Java compiler breaks the
source code into tokens during the lexical analysis phase of
compilation.
Types of Tokens in Java:
Keywords
Reserved words with special meaning in Java.
Examples: class, public, static, if, else, return, int, void
Identifiers
Names given to variables, methods, classes, etc.
Must start with a letter, underscore (_), or dollar sign ($);
followed by letters, digits, underscores, or dollar signs.
Examples: myVariable, Main, _count, $value
Literals
Constant values directly used in code.
Examples: 100, 3.14, 'A', "Java", true, null
Operators
Symbols that perform operations on variables and values.
Examples: +, -, *, /, =, ==, !=, ++, &&, ||
Separators (Delimiters)
Symbols used to separate code elements.
Examples:
; (statement terminator)
, (separator)
. (member access)
() (method call or grouping)
{} (block of code)
[] (array declaration/access)
Exercise Based Question:-
Q1. State whether it’s valid or invalid identifier.
a. Int -Valid
b. Void -Valid
c. System -Invalid
d. Double -Valid
e. string -Valid