EXAMPLES OF JAVA PROGRAM
1. class A
public static void main(String args[])
System.out.print(“hello”);
System.out.print(“hi”);
Note :- Class name is, basically, the program name where your program
is saved on the hard disk. Class name must not be a java reserved word.
But class name should be meaningful and relevant to your program.
Output:- hellohi
2.class B
public static void main(String args[])
System.out.println(“hello”);
System.out.println(“hi”);
}
}
Output:-hello
Hi
VARIABLES
Variables are the storage locations inside our computer memory where
any value can be stored. Since the value which is going to be stored is
not fixed, that is why it is called as variable.
A variable is a named memory location which contains a value. The
value of variable can change.
DATA TYPES
Data types are used to specify the size and type of value to be stored by
a variable. During programming we deal with various types of data. It
becomes necessary for a programmer to select an appropriate data
type according to the need of a program.
1.Primitive types/fundamental/basic/predefined data type:- These data
types are predefined or built-in nature.
Types
A. Numeric type: - it deals with numbers that may be integers or
fractional numbers.
Integers (or fixed point numbers):-whole numbers including positive
and negative which are written without using decimal points are called
integers e.g. 4,-8.
Floating numbers: - a fractional number which includes a positive or
negative value e.g. 4.5,-9.89.
B. Non-numeric type: - it deals with all alphabets, special characters or
digits which are non-numeric in nature.
Characters: - Each letter available on the keyboard is termed as a
character according to the ASCII system. A single letter is treated as a
character in Java language. There are 256 characters available in the
computer. In java characters are represented within single quotes like
‘a’.
String: - A set of characters used together is known as String. It may
be an alphanumeric or set of letters within a pair of opening and closing
double quotes.
DATA DATA TYPE BYTES FORMAT
(keywords)
Integer Int 4 bytes-32 int a;a=10;
bits
Long integer Long 8 bytes-64 long a;a=34556;
bits
Small range of float 4 bytes float a;a=34.56;
decimal value
Wide range of double 8 bytes double a;a=0.0000056;
decimal value
Character char 2 bytes char c; c=’a’;
More than String More than 2 String s;s=”computer”;
one character bytes
Boolean value boolean 1 bit boolean x=true;
Boolean: -It is a special type, where a variable contains either true or
false.
Data types in Java
1. Primitive types/basic/fundamental 2.Non-primitive
types/derived/reference
data type.
Classes
1. Numeric types 2.Non-numeric types
a) Integers a). characters Arrays
b) Floating numbers b). boolean
Interfaces
Identifiers
Identifier is a global term used to identify program element. Identifiers
represent names which can be assigned to variables, methods and
classes to uniquely identify them.
Declaration of variables
Syntax: - datatype <variable name>;
Where data type may be int, float, char, double, boolean etc.
variable names must be suitable name given by users but following
certain rules associated with naming.
semicolon ; is required to terminate a line. It marks end of line.
Eg: - int a,b;
char ch;
float sum,sub;
Rules for naming a variable: -
1. A variable may have any number of characters.
2. It may contain alphabets, digits, dollar $ sign, underscore _.
3. Variable name cannot begin with digit.
4. Variable names should be meaningful to easily depict the logic.
5. Variable names cannot match with the names of keywords.
6. True and false are predefined literals in java, so it cannot be used
as variable name.
7. Since java is case sensitive, so be attentive regarding the case of
variable names.
Valid identifiers: -
rollno, $1, A_Z,BASICSAL, _a etc.
Invalid identifiers: -
Student-roll-no( hyphen is not allowed)
Employee.data( dot . is not allowed)
9( only numbers cannot be used as a variable name)
12345
For( keyword)
8C(variable name cannot start with digit)
false(predefined boolean literal)
It means
int a,a1,a_1,a$1; all are valid identifiers.
But
int 1a,a-1,1a,a 1;all are invalid identifiers.