Simple But Must-Know Basic Things In Java

Data Types, Variables, and Methods in Java

Tharaka Rambukkamage
6 min readJul 4, 2023

“Understanding data types, variables, and methods is essential for any beginner learning Java. Data types determine the kind of values a variable can hold, variables store data for later use, and methods are blocks of code that perform specific tasks. Mastering these fundamental concepts forms the foundation for writing Java programs effectively.”

Data types in Java

Java supports several data types that can be used to store different kinds of values. Here are the main data types in Java:

1. Primitive Data Types:

  • boolean: Represents a boolean value (true or false).
  • byte: Represents a signed 8-bit integer.
  • short: Represents a signed 16-bit integer.
  • int: Represents a signed 32-bit integer.
  • long: Represents a signed 64-bit integer.
  • float: Represents a 32-bit floating-point number.
  • double: Represents a 64-bit floating-point number.
  • char: Represents a single Unicode character.
Primitive data types in Java
Result of the above code

2. Reference Data Types:

Can have null as the value

  • String: Represents a sequence of characters.
  • Arrays: Represents a collection of elements of the same type.
  • Classes: Represents user-defined types.
  • Interfaces: This represents a contract for classes to implement.
  • Enums: Represents a fixed set of values.
  • Wrapper classes: Provides a way to treat primitive types as objects (e.g., Integer, Boolean, etc.).

Note: Reference data types are passed by reference (the reference to the object is passed by value).

It’s important to note that Java is a strongly-typed language, which means that every variable and expression must have a declared type. Additionally, Java supports type inference with the var keyword starting from Java 10, where the type is inferred based on the assigned value.

Variables in Java

A variable is a storage location in a memory. Needs a name to refer to a memory location. The value of a variable can be changed during the execution. In Java, a variable is a named storage location that holds a value of a specific data type. Variables are used to store and manipulate data within a program. Here are some key points about variables in Java:

  1. Declaration: A variable is declared by specifying its data type, followed by the variable name. For example:
Declaration of a variable

This declares a variable named age of type int, which can hold integer values.

  1. Initialization: After declaring a variable, you can optionally initialize it with an initial value. For example:
Initialization
Result of the above code
print without an Initialization.
Result when printing without an Initialization

This declares a variable named age and initializes it with the value 24.

Important things to know when Initialization a variable.

a. Scope: The scope of a variable determines where it can be accessed within a program. Variables can have local scope, where they are declared inside a block or method and are only accessible within that block or method. They can also have the class scope, where they are declared as fields (also known as instance variables or class variables) and are accessible throughout the entire class.

b. Naming conventions: Variable names in Java must follow certain naming conventions. They should start with a letter (or underscore) and can include letters, digits, and underscores. Additionally, variable names are case-sensitive, meaning that age, Age, and AGE are considered different variables.

c. Assignment and re-assignment: Variables can be assigned values using the assignment operator (=). Once a variable is declared, its value can be changed or reassigned by assigning a new value to it.

d. Constants: A constant value cannot be changed after assigning. Use static and final modifiers. Use UPPERCASE with _ between words for naming

Declaration, Initialization, reassignment, and Final variable in Java
Result of the above code
when trying to change a final variable in Java.

Methods in java

In Java, methods are blocks of code that perform specific tasks and are executed when called or invoked. They allow you to organize and modularize your code by breaking it into reusable units. Methods are to represent objects’ behaviors.

There are four elements for method declaration in general.

1. Return type — The return type of a method specifies the type of value the method returns, if any. It can be any valid data type or void if the method doesn't return any value. For example, a method with a return type int would return an integer value.

2. Name- It’s essential to have a name

3. Pair of parentheses ( ) — Methods can accept zero or more parameters. Parameters are the inputs to the method, and their data types and names are specified in the method signature. Parameters allow you to pass values to the method to work with within the method body. Parameters pass inside in this pair of parentheses

4. Method body between braces {}- The method body contains the actual code that is executed when the method is called. It is enclosed within curly braces ({}) and can include any valid Java statements.

Four elements in methods declaration
How methods words in Java?
Result of the above code

Method Signature

In Java, a method signature refers to combining a method’s name and parameter list. It helps uniquely identify a method and specifies the types and order of the parameters the method accepts. The return type of the method is not part of the method signature.

Name, Number, and type of its parameters, in order.

Method Signature

In the above example, the method signature is:

  • Method name: calculateSum
  • Parameter list: int num1, int num2
  • Return type: int

Method invoking

In Java is the process of calling or executing a method to perform a specific task. You invoke a method by using its name followed by parentheses () and any required arguments inside the parentheses. When a method is invoked, it executes its code and may optionally return a value. To invoke a method without a return value, you simply call the method and pass any required arguments. If the method has a return value, you can assign the returned value to a variable of the appropriate type.

Method Overloading

Java allows you to define multiple methods with the same name but different parameter lists. This is called method overloading. Overloaded methods have different parameter types, numbers, or orders.

Access Modifiers

Methods can have access modifiers such as public, private, or protected, which define their accessibility from other parts of the code.

  1. public: The public access modifier allows the class, method, variable, or constructor to be accessed from anywhere in the program, including other classes and packages.
  2. private: The private access modifier restricts the visibility of the class, method, variable, or constructor to only within the same class. It cannot be accessed or modified from outside the class.
  3. protected: The protected access modifier allows the class, method, or variable to be accessed within the same package or by subclasses in different packages.
  4. Default (no modifier): If no access modifier is specified, it is considered the default access level. It allows access within the same package but restricts access from classes in different packages

In Java, a strong grasp of data types, variables, and methods is essential for mastering the language. Data types allow for effective manipulation of various data, variables enable storage and retrieval of values, and methods provide structure and reusability to code. By understanding these foundational concepts, developers can lay a solid groundwork for building robust and efficient Java programs, paving the way for further exploration and proficiency in the language.

--

--

Tharaka Rambukkamage

Undergraduate | Bachelor of Science (Hons) Information Technology | University Of Moratuwa