Monday, September 19, 2022

Java is a Strongly Typed Language

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

  1. Java Pass by Value or Pass by Reference
  2. Java Automatic Numeric Type Promotion
  3. Why Class Name And File Name Should be Same in Java
  4. Why main Method static in Java
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Object Creation Using new Operator in Java
  2. super Keyword in Java With Examples
  3. Difference Between Abstract Class And Interface in Java
  4. Check Given Strings Anagram or Not Java Program
  5. ArrayList in Java With Examples
  6. Spring Java Configuration Example Using @Configuration
  7. Angular First App - Hello world Example
  8. Python continue Statement With Examples

No comments:

Post a Comment