One thing about Java which you should know before you dig any deeper is that Java is a strongly typed language. What it means is that every variable must be declared with a proper type before those variables can be used.
<type> <variable>;
Here type may be a primitive data type or a non-primitive data type (user defined type i.e. a class).
Here are some examples of variable declaration using primitive types
int x;
float f;
Here are some examples of variable declaration using non-primitive types (some class)
Employee e;
ArrayList numList;
Scanner sc;
But that is not all that makes Java strongly typed, each assignment and parameter passing to the method is checked for type compatibility.
For example, a variable once declared as int can only be assigned an Integer value.
int x = 5; // correct assignment is compatible to variable type x = 10; // correct new assignment is compatible to variable type x = 10.92; // error trying to assign decimal value to an int type variable x = "test"; // error trying to assign String value to an int type variable
Same way for classes where assignment is a bit different because you actually create an object of the class but still it has to be of compatible type.
Employee emp = new Employee();
Refer Class in Java for more clarity on how class is created and how object of a class is defined.
Same way as an example let's say you have a method sum in a class as defined below
public class Test { public static void main(String[] args) { Test t = new Test(); t.sum(5.6, 6.7); t.sum("a", "b"); // Error trying to pass parameters of incompatible type t.sum(4, 5); } public void sum(double a, double b){ double r = a + b; System.out.println("Sum is- " + r); } }
As you can see method sum takes two arguments of type double so first call to the method is correct but second call where two parameters of type String are passed results in an error.
You may wonder why third call where two parameters of type int are passed works? That is because both int and double are ultimately numbers and there is something called widening type conversion which takes care of this parameter passing.
Refer Type Casting in Java With Conversion Examples to know more about type conversion in Java.
Advantages of strongly typed language
One of the main advantages of strongly typed language is having strict type checking at the compile time itself. So that at run time you don't have exceptions thrown for incompatible types.
Java 10 introduced a new feature called local variable type inference where the type of the variable is inferred from the variable initializer. A new reserved type name "var" is added in Java to define and initialize local variables, read more about var type here- Var type in Java - Local Variable Type Inference
That's all for this topic Java is a Strongly Typed Language. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment