Date : 07-April-2025
Java Basic things:
In Java only two types of symbols cam end the block of code or puttig end to the statement.
One is -> ;
anothher one is -> { }
These are the two types of way where we can putting an end to our statements in Java.
Method return data types:
We can return both primitive and non-primitive data types from method.
Additionally, we can specify a special return type called void.
Void means -> nothing to return.
If a method is declared using void as return type it may or mah not use return statement without value.
Buf if we returns a value in a void function, Java throws an Compile Time Error.
Default values.
What is default values?
If we declared a variable without any value in global scope, JVM fill the variables with a default value according to its datatype.
Important Note:
This default value concept only works on global variables(static variables and Instance variables).
JVM does not apply this concept on local variables.
Only Global variables(instance variables) can be able to avail this feature.
Reason behind this Default values:
Instance variables only declared and initiated when the respective object wants to initiate them. Until the object was created variables cannot have values. Hence JVM puts the default values in it.
Static variables can also have default values by JVM.
+---------+---------------+
|Datatype | Default values|
+---------+---------------+
int - 0
short - 0
byte - 0
long - 0
float - 0.0
double - 0.0
char - ''
boolean - false
Object
reference - null
- By default the decimal number are considered as double in java not float. e.g,
float piValue(){
return 3.14; -> It throws error because by default decimal nums considered as double.
}
we need to give like this
float piValue(){
return 3.14f; -> it works fine
}
- Like float, by default the whole numbers in Java sre considered to be as Integer datatype.
Impicit Type conversion in java.
The smaller sized datatypes can be casted into larger sized datatypes but the vice versa cannot be done due to size limitation.
e.g,
Correct ✓ byte,short, int -> long.
Wrong X long -> int, short, byte.
This condition also fit for float & double data types.
Top comments (0)