DEV Community

Cover image for Java 101: Understanding Variables, Data Types, and Input/Output
Pavan Varma
Pavan Varma

Posted on

Java 101: Understanding Variables, Data Types, and Input/Output

Introduction:

Java is one of the most popular and versatile programming languages in the world, and it is used in everything from web applications to mobile apps. Understanding the basics is essential if you're starting your journey into Java. In this guide, we’ll dive into three foundational concepts—variables, data types, and input/output operations—which form the backbone of any Java program. By the end of this article, I promise that you will have a clear understanding of Java and its basics.

Types of Languages:

Before we deep dive into learning Java, we must first understand how many types of languages there are. There are three types of languages, they are

Types of Languages

Procedural:

  • Procedural language specifies a series of well-structured steps and procedures to compose a program.

  • It Contains a systematic order of statements, functions, and commands to complete a task.

Functional:

  • Writing a program entails using pure functions, which means never modifying variables but only creating new ones as output.

  • Used in situations where we have to perform lots of different operations on the same set of data.

Object-Oriented:

  • Resolves around objects.

  • Code + Data = object.

  • Developed to make it easier to develop, debug, reuse, and maintain
    software.

In Conclusion to this section,

“Java is predominantly an object-oriented language, but it also supports procedural and functional programming to some extent. ”

What is Java?

Java is a static programming language created by James Gosling at Sun Microsystems in 1995, Java is known for its simplicity, reliability, and portability, making it a favorite among developers for building everything from mobile apps to large-scale enterprise systems.

How Java code executes

Before Java came into the picture languages like C, and C++ they are platform-dependent, but Java is platform-independent, which means the source code that we write will be converted into byte code.

A compiler helps in doing this by turning it into executable code. This code is a set of instructions for the computer. Now, the JVM (Java Virtual Machine) converts this byte code to machine code. Here, JVM is platform-dependent.

Java Executes

Architecture:

Architecture

Writing your First Java Program:

Before diving into the main topics let’s start by writing your first Java program. A simple program that prints “Hello World!” is a great way to start programming and it’s a universal program. Anyone who starts to learn programming will start their journey by writing this program.

Step-1:

Before you write your first Java program, make sure you have the Java Development Kit (JDK) installed on your system. You can download it from Oracle’s website if you haven’t already.

You also need an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA to write and run your Java programs. I use Intellij

Step-2:

Now, let’s write a simple Java program that prints “Hello World!” the console.

// This is a simple Java program
public class Main {
    public static void main(String[] args) {
        // Prints "Hello, World!" to the console
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step-3:

Now, run the code in your IDE you can get the output as:

Hello, World!

Also, I will provide a coding question link for you to solve your first problem in Java from GeeksforGeeks.

Coding Question link here: Hello World

Let’s move on to the main topics…,

Variables:

Variables are containers that hold data values. In Java, every variable has a type that defines the kind of data it can store.

variable

We can declare and initialize the variable in one step;

Data Types:

Type of data that can be stored in the variable. There are two Data types in Java, they are:

  • Primitive

  • Non-Primitive

Primitive Data type:

It means any data type that cannot be broken further.

Java has 8 primitive data types: int, byte, short, long, float, double, boolean, and char.

public class Test {
    public static void main(String[] args) {
        // Primitives
        boolean isValid = true;
        byte marks = 90;
        int num = 10;
        float weight = 70.5F;
        long views = 1_000_000_000;
        char gender = 'M';
    }
}
Enter fullscreen mode Exit fullscreen mode

Non-Primitve Data type:

  • Non-Primitive type variables are always references.

  • Memory for Non-primitives is always allocated on the heap.

  • Members of Non-primitives get default values.

public class NonPrimitives {
    public static void main(String[] args) {
        // Non primitives in java
        String s = "GeeksforGeeks";
        // others are class, object, interface, Array
        System.out.println(s);
    }
}
Enter fullscreen mode Exit fullscreen mode

Wrapper Classes in Java:

A Wrapper class is a class whose object wraps (or) contains primitive data types. When we create a field and in this field, we can store primitive data types. In easy words, we can wrap a primitive value into a wrapper class object.

  • Character

  • Byte

  • Short

  • Integer

  • Long

  • Float

  • Double

  • Boolean

Type Conversion (or) Types Casting:

If the data types are compatible, then Java will perform the conversion automatically known as Automatic type conversion, and if not then they need to be cast (or) converted explicitly.

Widening (or) Implicit Conversion:

Widening conversion takes place when two data types are automatically converted. This happens when,

  • The two data types are compatible.

  • When we assign a value of a smaller data type to a bigger data type

Implicit conversion

*Narrowing (or) Explicit Conversion:
*

If we want to assign a value of a larger data type to a smaller data type we perform explicit conversion (or) narrowing

This is useful for incompatible data types where automatic conversion cannot be done.

Explicit Conversion

Input and Output:

Input:

To give some value to the system or a computer form the user is known as Input.

Java has two types of Input:

  • Buffered Reader

  • Scanner

Buffered Reader:

The Buffered Reader class reads text from an input stream, buffering characters for efficient reading. While it’s often used for file input, it can also be used for reading user input from the console.

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        // BufferedReader example
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // String example
       System.out.print("Enter a String: ");
       String s = br.readLine();
       System.out.println("You Entered: " + s);
        // Integer example
        System.out.print("Enter a Integer: ");
        int x = Integer.parseInt(br.readLine());
        System.out.println("You Entered: " + x);
    }
}
Enter fullscreen mode Exit fullscreen mode

Scanner:

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.

import java.util.Scanner;
public class ScannerExample {
    public static void main(String[] args) {
        // Scanner Example
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter: ");
//        String sr = sc.nextLine();
        int n = sc.nextInt();
        System.out.println(n);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

After giving the input we receive an value or something known as output. In Java we use;

System.out.println();
System.out.print();
System.out.printf();
Enter fullscreen mode Exit fullscreen mode

Conclusion:

In this article, we have explored some of the fundamental concepts of Java, Including writing your first program. Mastering these basics is a crucial step in becoming comfortable with Java and programming in general.

And that’s it we completed learning about Java fundamentals and in the next article I’m gonna help you to learn more concepts of Java.

Keep pushing forward and STAY HARD!!

If you liked the article share it with your friends and give a like and follow.

See ya :)

Top comments (0)