-
Here, we will know about the fundamental of Java programming language and platform, which provides a broad overview of what this technology can do and how it will make your life easier.
-
There is discussion on the "Hi I am James Bond!" application, describing each section of code in detail. It covers source code comments, the "MainClass" application definition block, and the main method.
What are the number of keywords available in the Java programming language. Here is the list so, You cannot use any of the following as identifiers in your programs.
class |
extends |
byte |
public |
synchronized |
interface |
implements |
short |
private |
transient |
package |
import |
int |
protected |
volatile |
this |
super |
long |
new |
abstract |
if |
instanceof |
float |
void |
final |
else |
try |
double |
return |
static |
for |
catch |
boolean |
case |
native |
do |
throw |
char |
assert ### |
enum #### |
switch |
throws |
break |
strictfp ## |
const |
while |
finally |
continue |
default |
goto |
Note:- Don’t Use const and goto in program.
Note:- false, true and null are literal types keywords.
## |
|
added in 1.2 |
### |
|
added in 1.4 |
#### |
|
added in 5.0 |
- An identifier is the name given by a programmer to a variable, statement label, method, class, and interface.
- An identifier must begin with a letter, $ or _
- Subsequent characters must be letters, numbers, $ or _
- An identifier must not be a Java keyword
- Identifiers are case-sensitive
Example:
Invalid |
Valid |
int 1num,2num,3num; |
int num1,num2,num3; |
class Daily&Sales{} |
class Daily_Sales{} |
float new=12.5f; |
float New=12.5f; |
Note:- dispDate() is not the same as DispDate().
- A literal is a representation of a value of a particular type.
Type |
Examples & Values |
boolean |
true, false |
character |
’a’ ‘\uFFFF’ ‘\777’ |
integer |
123 123L O123 Ox123 |
floating-point |
123.5 123.5D 123.5F 123.5e+6 |
object |
“test” null |
escape sequences |
\n \t \b \f \r \’ \” \\ |
JavaScript (weakly typed)
1: var x; // Declare a variable
2: x = 1; // Legal
3: x = "Test"; // Legal
4: x = true; // Legal
Java (strongly typed)
1: int x; // Declare a variable of type int
2: x = 1; // Legal
3: x = "Test" // Compiler Error
4: x = true; // Compiler Error
Primitive Data Type
- Primitive data types represent atomic values and are built-in to Java
- Java has 8 primitive data types
Type |
Bits |
Lowest Value |
Highest Value |
boolean |
N/A |
false |
true |
char |
16 |
‘\u0000’ [0] |
‘\uffff’ [216-1] |
byte |
8 |
-128 [-27] |
+127 [27-1] |
short |
16 |
-32,768 [-215] |
+32,767 [215-1] |
int |
32 |
-2,147,483,648 [-231] |
+2,147,483,647 [231-1] |
long |
64 |
-9,223,372,036,854,775,808 [-263] |
+9,223,372,036,854,775,807 [263-1] |
float |
32 |
±1.40129846432481707e-45 |
±3.40282346638528860e+38 |
double |
64 |
±4.94065645841246544e-324 |
±1.79769313486231570e+308 |
For finding the range of the value for certain datatype, following equation is used:-
syntax :-
-2n-1 To +2n-1-1
e.g
byte takes 8 bits so range can be calculated as :
-28-1 To +28-1-1
-27 To +27-1
-128 To +127
Reference Data Types :
- Reference data types represent objects
- A reference serves as a handle to the object, it is a way to get to the object
Java has 2 reference data types:-
The following chart summarizes the default values for the above data types.
Data Type |
Default Value (for fields) |
byte |
0 |
short |
0 |
int |
0 |
long |
0L |
float |
0.0f |
double |
0.0d |
char |
‘\u0000’ |
String (or any object) |
null |
boolean |
false |
The Java programming language defines the different kind of variables:
- Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words);
- Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
- Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (as for example, int ctr = 0; ). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
- Parameters :- The signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields".
Declaring a variable with primitive data type:-
Declaring a variable with reference data type:-
- Here, How operators work in Java programming language. It presents the most commonly-used operators first, and the less commonly-used operators last. Each discussion includes code samples that you can compile and run.
- Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks. This section discusses expressions, statements, and blocks.
- Unary operators use only one operand
Increment by 1, can be prefix or postfix |
++ |
Decrement by 1, can be prefix or postfix |
— |
Negative sign |
– |
Positive sign |
+ |
- Arithmetic operators are used for basic mathematical operations
Add |
+ |
Subtract |
– |
Multiply |
* |
Devide |
/ |
Modulo, Remainder |
% |
- String operator (+) is used to concatenate operands
- If one operand is String, the other operands are converted to String
- Relational operators are used to compare values
- boolean values cannot be compared with non-boolean values
- Only object references are checked for equality, and not their states
- Objects cannot be compared with null
- null is not the same as “”
Less than |
< |
Less than or equal to |
<= |
Greater than |
> |
Greater than or equal to |
>= |
Equals |
== |
Not equals |
!= |
- The ternary operator (?:) provides a handy way to code simple if-else() statements in a single expression, it is also known as the conditional operator
- If condition is true, then exp1 is returned as the result of operation
- If condition is false, then exp2 is returned as the result of operation
- Can be nested to accommodate chain of conditions
- Syntax:-
condition ? exp1 : exp2 ;
- Logical operators are used to compare boolean expressions
- ! inverts a boolean value
- & | evaluate both operands
- && || evaluate operands conditionally.
NOT |
! |
AND |
& |
OR |
| |
XOR |
^ |
Short-circuit AND |
&& |
Short-circuit OR |
|| |
Truth Table :-
Op1 |
Op2 |
!Op1 |
Op1 & Op2 |
Op1 | Op2 |
Op1 ^ Op2 |
Op1 && Op2 |
Op1 || Op2 |
false |
false |
true |
false |
false |
false |
false |
false |
false |
true |
true |
false |
true |
true |
false |
true |
true |
false |
false |
false |
true |
true |
false |
true |
true |
true |
false |
true |
true |
false |
true |
true |
Short-circuit Logical Operators (&&,|| and !)
Similar to the Boolean operators, but with an added ability to “short-circuit” part of the process, using a couple of mathematical rules:
- If the left operand of an && operation is false, the result is automatically false, and the right operand is not evaluated
boolean a = (15>18) && (18>15);
If the left operand of an || operation is true, the result is automatically true, and the right operand is not evaluated
boolean b = (18>15) || (15>18);
Boolean Complement ( ! ):
The NOT function inverts the value of boolean
boolean b=true
boolean c = !b;
- Assignment operators are used to set the value of a variable
Assignment |
= |
Add and assign |
+= |
Subtract and assign |
-= |
Multiply and assign |
*= |
Divide and assign |
/= |
Modulo and assign |
%= |
AND and assign |
&= |
OR and assign |
|= |
XOR and assign |
^= |
- Shift Operator :
- Right Shift Operator >>
- Left Shift Operator <<
- Evaluation order of operators in Java is as follows:
- Unary (++ — + – ~ ())
- Arithmetic (* / % + –)
- Shift (<< >> >>>)
- Comparison (< <= > >= instanceof == !=)
- Bitwise (& ^ |)
- Short-circuit (&& || !)
- Conditional (?:)
- Assignment (= += -= *= /=)
TypeCasting is converting from one data type to another
- Implicit casting : is an implied casting operations
- Explicit casting : is a required casting operations
-
Primitive casting : is converting a primitive data type to another
–Widening conversion : is casting a narrower data type to a broader data type
–Narrowing conversion : is casting a broader data type to a narrower data type
-
Reference casting : is converting a reference data type to another
–Upcasting : is conversion up the inheritance hierarchy
–Downcasting : is conversion down the inheritance hierarchy
-
Casting between primitive and reference type is not allowed
-
boolean type can’t be typecasted.
-
In Java, casting is implemented using () operator.
Example1:-
Example2:-
- Here, How the control flow statements supported by the Java programming language; we will see.
- It covers the decisions-making, looping, and branching statements that enable your programs to conditionally execute particular blocks of code.
- if-else() performs statements based on two conditions
- Condition should result to a boolean expression
- If condition is true, the statements following if are executed
- If condition is false, the statements following else are executed
- Can be nested to allow more conditions
- Syntax :-
if (condition) {
// braces optional in case of multiple statement : required
// statement required
}
else {
// else clause is optional
// statement required
}
- switch() performs statements based on multiple conditions
- exp can only be char, byte, short, int, and enum val should be a unique constant of exp
- case statements falls through the next case unless a break is encountered
- default is executed if none of the other cases match the exp and it is optional.
- Syntax :-
switch (exp) {
case val:
// statements here
case val:
// statements here
default:
// statements here
}
- while() performs statements repeatedly while condition remains true
- Syntax : –
while (condition) {
// braces optional in case of multiple statement : required
// statements here
}
- do-while() performs statements repeatedly (at least once) while condition
remains true
- Syntax :-
do
{
// statements here
}while (condition);
- for() performs statements repeatedly based on a condition
- Init is a list of either declarations or expressions, evaluated first and only once
- Condition is evaluated before each iteration
- Exp is a list of expressions, evaluated after each iteration
- All entries inside () are optional, for(;;) is an infinite loop
- Syntax:-
for (init; condition; exp) {
// braces optional in case of multiple statement : required
// statements here
}
- break exits loops and switch() statements
- Syntax:-
break;
- continue is used inside loops to start a new iteration
- Syntax :-
continue;
- A label is an identifier placed before a statement, it ends with :
- break labelName is used to exit any labelled statement
- continue labelName is used inside loops to start a new iteration of the labeled loop
- Syntax:-
labelName:
break labelName;
continue labelName;
- return branching statement is used to exit from the current method.
- Two forms for return keyword:
- Syntax:-
return <value>;
return;
Java Online Resources
Tutorials:
FAQs:
Recent Comments