Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Java SE 21 Developer (Exam 1Z0-830) - Core Java SE Basics

Learn the fundamentals of Java programming in this hands-on Code Lab designed for beginners. Build a strong foundation by writing and running Java programs, using primitive and date-time data types, and performing essential input and output operations. By the end of this lab, you’ll be confident in creating basic Java programs, setting the stage for more advanced concepts.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 44m
Published
Clock icon Feb 28, 2025

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Introduction

    In this lab, you will gain hands-on experience with core Java concepts. You will learn how to write and run Java programs, use primitive data types, manipulate strings, handle date-time data, and perform input and output operations.


    Java

    Java is a high-level, object-oriented programming language designed for flexibility, reliability, and portability. Java programs are compiled into bytecode that runs on the Java Virtual Machine (JVM), making Java platform-independent.

    Key features of Java include:

    Simplicity: Java's syntax is similar to C and C++ but eliminates complex features like pointers and multiple inheritance.

    Object-Oriented: Java uses objects and classes to structure programs efficiently.

    Platform Independence: Java code runs on any device with a JVM.

    Automatic Memory Management: Java handles memory allocation and garbage collection automatically.


    In the next step you will understand how to compile and run java code.

  2. Challenge

    Compile and Run Basic Java Program

    Java is one of the most widely used programming languages in the world, known for its simplicity, portability, and scalability. It is used in web applications, mobile development, enterprise systems, and more.

    Java follows the Write Once, Run Anywhere (WORA) principle, which means that Java applications can run on any platform that has a Java Virtual Machine (JVM). This is achieved by compiling Java source code into bytecode, which is executed by the JVM.


    JVM and Bytecode

    When you write a Java program and compile it using javac, it generates a .class file containing bytecode. This bytecode can run on any machine with a JVM.

    Compile Java program :

    javac Example.java
    

    This command generates a file named Example.class

    Run Java Program :

    java Example
    

    info> When running the class file you give the class Name without the .class extension.

    Now you have to compile the IntroJava.java file. Great, now that file is compiled you can check the generated class by typing ls Intro* command in the Terminal.

    You will be able to see two files

    Introjava.java
    IntroJava.class
    

    Now you can execute the class file. The main method serves as the entry point of a Java program.
    When you execute the class file, Java automatically invokes the main method.

    Expected Output:

    Welcome to Java SE 21 Basics!
    

    Now that you've learned how to compile and run a Java class, next, you'll explore basic data types in Java.

  3. Challenge

    Printing Output to the Console

    In the last step, you looked at how to compile and run java program and also how to print a message on console using System.out.println.

    Now you'll understand more about System.out.

    Java provides the System.out object to output text to the console. This is one of the simplest ways to display information, debug, and interact with users.

    System.out provides several methods for output shown below.

    | Java Output Method | Description | | -------- | -------- | | System.out.println() | Prints text followed by a newline. | | System.out.print() | Prints text without a newline. | | System.out.printf() | Prints formatted text similar to C's printf. |

    Now you can try printing your name using System.out.print. You can now compile and run the ConsoleOutput.java file.

    You will get an output similar to below ( Considering your your name is John Doe ) :

    Hello John Doe welcome to java !
    

    The output was printed on the same line.

    Now you will use the println method which adds a newline after printing. As seen in the previous tasks, the key difference between print and println is that println adds a new line after printing the statement.

    Next you'll learn about printf for formatted output. ---
    The printf() method in Java formats and displays output in a structured manner, allowing precise control over how numbers, strings, and other data types are presented using format specifiers.

    Printing with print:

    System.out.print("Hello " + name + " you are " + age + " years old.");
    

    Using printf(), the above statement can be rewritten as:

    System.out.printf("Hello %s you are %d years old.", name, age);
    

    In this step, you learned about how to print the output to the console using print , println and printf, in the next step you will learn about the primitive data types in Java.

  4. Challenge

    Primitive Data Types in Java

    Primitives are the basic data types in Java that store simple values directly in memory. They are essential for handling numbers, characters, and boolean values efficiently. Java provides eight primitive data types, each designed for specific types of values.

    Primitive variables fall into four main categories:

    1. Integer Types for whole numbers

    • byte, short, int, long

    2. Floating-Point Types for decimal values

    • float, double

    3. Character Type for single chracters

    • char

    4. Boolean Type for true/false values

    • boolean

    | Type | Size (bits) & Values | Example | |-----------|-----------------------------------|-----------------| | byte | 8-bit
    Min: -128
    Max: 127 | byte b = 100; | | short | 16-bit
    Min: -32,768
    Max: 32,767 | short s = 32000; | | int | 32-bit
    Min: -2.1 × 10⁹
    Max: 2.1 × 10⁹ | int i = 100000; | | long | 64-bit
    Min: -9.2 × 10¹⁸
    Max: 9.2 × 10¹⁸ | long l = 100000L; | | float | 32-bit
    Min: -3.4 × 10³⁸
    Max: 3.4 × 10³⁸ | float f = 5.75f; | | double | 64-bit
    Min: -1.7 × 10³⁰⁸
    Max: 1.7 × 10³⁰⁸ | double d = 19.99; | | char | 16-bit
    Min: 0
    Max: 65,535 | char c = 'A'; | | boolean | 1-bit
    Values: true or false | boolean flag = true; |


    Each primitive type has a default value:

    • Numeric types default to 0.
    • boolean defaults to false.
    • char defaults to null character.

    In Java, you can initialize a variable with an initial value when declaring it, or you can declare the variable first and assign a value later.

    // Declare and initialize a variable
    // i is assigned the value 10 at the time of declaration
    int i = 10; 
            
    // Declare a variable without initialization
    int j;
    
    // Assign a value later
    // j is now assigned the value 20
    j = 20; 
    
    // Print values
    System.out.println("i: " + i);
    System.out.println("j: " + j);
    

    Output

    i: 10
    j: 20
    ``` ---
    
    #### Different ways to define a `long`
    
    ```java
    long myLong1 = 100;
    long myLong2 = 100l;
    long myLong2 = 100L;
    
    System.out.println("Long Value1: " + myLong1);
    System.out.println("Long Value2: " + myLong2);
    System.out.println("Long Value3: " + myLong3);
    
    

    Output :

    Long Value1: 100
    Long Value2: 100
    Long Value3: 100
    

    Understanding widening conversion

    info> Widening conversion (implicit type casting) is the automatic conversion of a smaller data type to a larger one without data loss, as the larger type can fully accommodate the smaller value.

    • In the above example, myLong1 is assigned the value 100, which is an int literal by default. Java implicitly converts it to long through widening conversion.
    • myLong2 and myLong3 use explicit long literals (l and L respectively). These tell Java that the number should be treated as a long from the start.

    info> Best Practice: When specifying a long value, use an uppercase "L" rather than a lowercase "l" to prevent it from being confused with the number 1. Like long values use the L suffix, floating-point numbers can include type indicators: d for double (optional) and f for float (required when using decimal values).

    Now you will define a double variable named myDouble. Now in the next task update the value of myDouble to 400.55. Now you'll learn about the char primitive type.

    The char primitive type in Java is used to store a single character. It is a 16-bit Unicode character, meaning it can represent letters, digits, symbols, and special characters from the Unicode standard.

    Example:

    char letter = 'A';   // Character literal
    char number = '5';   // Digit character (not a number)
    char symbol = '@';   // Special character
    char unicodeChar = '\u2764'; // Unicode for '❤'
    
    System.out.println("Letter: " + letter);
    System.out.println("Number as char: " + number);
    System.out.println("Symbol: " + symbol);
    System.out.println("Unicode Character: " + unicodeChar);
    
    

    Output:

    Letter: A
    Number as char: 5
    Symbol: @
    Unicode Character: ❤
    ``` Now, compile and run the program as demonstrated in the earlier steps.  
    
    You will observe that both `myChar1` and `myChar2` print the value **'A'**, confirming that assigning `65` to a `char` results in the corresponding Unicode character. ---  
    In this step, you explored Integer, Floating-Point, Character, and Boolean primitives in Java.  
    
    Next, you'll learn how to manipulate and work with text using Strings.
  5. Challenge

    Working with Strings

    Strings are a fundamental part of Java programming. They allow developers to handle text-based data efficiently. Unlike primitive data types, strings in Java are objects of the String class and are immutable.

    In Java, String immutability means that once a String object is created, its value cannot be changed. Any operation that appears to modify a String actually creates a new String object instead of modifying the original one.

    Java provides a wide range of methods for manipulating strings. Strings are enclosed in double quotes ("") and can be concatenated, compared, searched, and modified using built-in methods.


    Creating Strings

    A String can be created and initialized using the following :

    String message = "Hello, Java!";
    

    String Methods

    Some commonly used methods in the String class:

    • .concat(String str) - Concatenates two strings.

    • .length() - Returns the number of characters in the string.

    • .toUpperCase() / .toLowerCase() - Converts all characters to uppercase or lowercase.

    • .substring(int start, int end) - Extracts a portion of a string.

    • .indexOf(String str) - Finds the first occurrence of a substring.


    Concatenating Strings

    String salutation = "Mr.";
    String name = "John Doe";
    
    // Concatenation using +
    String fullName = salutation + name;
    System.out.println("Full Name: " + fullName);
    
    

    Output

    Full Name: Mr.John Doe
    

    Alternatively you can also use .concat for appending

    Example

    String fullName= salutation.concat(name);
    ``` Next you will try the other functions such as `.length()` , `toUpperCase()` and `toLowerCase()`.
    
     ---
    
    ### Comparing strings in java 
    
    In Java, strings can be compared in multiple ways, depending on whether you need a case-sensitive or case-insensitive comparison. The three most common methods are:
    
    - Using `equals()` (Case-Sensitive)
    - Using `equalsIgnoreCase()` (Case-Insensitive)
    - Using `compareTo()` (Lexicographical Comparison)
    
    ---
    
    #### equals()
    
    The equals() method compares the content of two strings and returns true if they are identical, considering case.
    
    ```java
    String str1 = "Java";
    String str2 = "Java";
    
    System.out.println(str1.equals(str2)); 
    

    Output

    true
    

    equalsIgnoreCase()

    Similarly equalsIgnoreCase() performs a case insensitive search.

    String str1 = "Java";
    String str2 = "JAVA";
    
    System.out.println(str1.equalsIgnoreCase(str2)); 
    

    Output

    true
    

    compareTo() (Lexicographical Order)

    The compareTo() method compares two strings character by character and returns:

    • 0 → If both strings are equal.
    • A negative number → If the first string is lexicographically smaller.
    • A positive number → If the first string is lexicographically larger.

    Example :

    String str1 = "Apple";
    String str2 = "Carrot";
    String str3 = "Apple";
    
    System.out.println(str1.compareTo(str2)); // Negative (Apple < Carrot)
    System.out.println(str1.compareTo(str3)); // 0 (Apple == Apple)
    System.out.println(str2.compareTo(str1)); // Positive (Carrot > Apple)
    

    Output:

    -2
    0
    2
    

    Now you will check if the fileName and fileName2 are equal using .equals Now, compile and run the LearningStrings.java file.

    Feel free to explore and experiment with different string functions in Java.

  6. Challenge

    Handling Date and Time in Java

    Java provides the java.time package to handle date and time efficiently. This package includes classes for dates, times, and timestamps while offering various methods for formatting, calculations, and manipulation.


    Key Classes in java.time Package

    | Class & Description | Example | |-------------------------------------------------------------|----------------------------------| | LocalDate
    Represents a date (year, month, day) without time | LocalDate.now()
    Output: 2024-06-15 | | LocalTime
    Represents a time (hour, minute, second) without date | LocalTime.now()
    Output: 14:30:15 | | LocalDateTime
    Represents both date and time | LocalDateTime.now()
    Output: 2024-06-15T14:30:15 | | DateTimeFormatter
    Formats and parses date-time values | DateTimeFormatter.ofPattern("yyyy-MM-dd") |

    Open the file LearningDateTime.java.

    Review the examples of LocalDate and LocalTime variables.

    Compile and Run the file LearningDateTime.java to see the output.

    You will get an output similar to below:

    Current Date: 2025-02-24
    Current Time: 00:42:53.599762900
    

    Next, you will define a LocalDateTime variable named dateTime. Well done!

    After running the program, your output should look similar to:

    Current Date-Time: 2025-02-24T00:53:47.706460
    

    By default, LocalDateTime prints in ISO format (yyyy-MM-ddTHH:mm:ss).

    Next, you'll format the date-time to your preferred format using DateTimeFormatter.

    Example of DateTimeFormatter:

    LocalDateTime now = LocalDateTime.now();
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
    String formattedDateTime = now.format(formatter);
    
    System.out.println("Formatted Date-Time: " + formattedDateTime);
    
    

    You only need the date along with hours and minutes, and your desired format is:

    dd-MM-yyyy HH:mm

    Now, you will format the dateTime variable to display the date and time in this specified format. In this step, you worked with LocalDate, LocalTime, and LocalDateTime to retrieve and display the current date and time. You also formatted date-time values using DateTimeFormatter to customize the output.

    In the next step, you'll learn how to handle user input in Java.

  7. Challenge

    Handling User Input in Java

    In Java, user input is typically handled using the Scanner class from the java.util package. It allows programs to read input from various sources, including the keyboard (console input), files, and streams.


    Scanner

    Scanner class provides the following methods:

    | Method | Description | |-----------------|--------------------------------| | nextLine() | Reads a full line (including spaces) | | nextInt() | Reads an integer | | nextDouble() | Reads a decimal number | | nextBoolean()| Reads a boolean (true/false) | Example :

    Scanner scanner = new Scanner(System.in); 
    
    System.out.print("Enter your name: ");
    String name = scanner.nextLine(); // Read a full line
    
    System.out.print("Enter your age: ");
    int age = scanner.nextInt(); // Read an integer
    
    System.out.println("Hello, " + name + "! You are " + age + " years old.");
    
    scanner.close(); // Close the scanner
    

    Output:

    Enter your name: John Doe
    Enter your age: 25
    Hello, John Doe! You are 25 years old.
    ``` Now read the **Price** from input. Good work !
     
     Now you can compile and run the file `LearningUserInput.java`.
      
    In this step, you learned how to handle user input in Java using the `Scanner` class, read different data types.
  8. Challenge

    Conclusion and Next Steps

    In this lab, you have gained hands-on experience with fundamental Java concepts, including:

    • Writing and executing basic Java programs.
    • Understanding and working with primitive data types.
    • Performing string operations and printing output to console.
    • Managing date and time using the java.time package.
    • Handling user input using the Scanner class.

    By applying these concepts, you have built a strong foundation in Java programming that will help you develop more complex applications.


    Next Steps

    • Explore control flow structures such as loops (for, while) and conditionals (if-else, switch).
    • Understand methods and functions for reusable and modular code.
    • Get familiar with object-oriented programming (OOP) principles such as classes, objects, and inheritance.
    • Learn about arrays and collections to store and manipulate multiple values efficiently.

    Keep practicing and experimenting with Java to deepen your understanding and improve your coding skills.

Amar Sonwani is a software architect with more than twelve years of experience. He has worked extensively in the financial industry and has expertise in building scalable applications.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.