-
Notifications
You must be signed in to change notification settings - Fork 0
Module 1 Java Syntax Mastery Easy
Welcome to the first step in your Java learning journey! This page covers the foundational elements of Java programming, focusing on basic syntax and fundamental programming concepts. By the end of this section, you should be comfortable with writing simple Java programs and understanding Java code.
Java is a powerful, versatile, and widely used programming language. It operates on the principle of "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
Every journey begins with a single step. In programming, that step is often a "Hello, World!" program. Here's how it looks in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Comments are used to explain Java code, and to make it more readable. They are not executed as part of the program.
// This is a single-line comment
/*
This is a multi-line comment
that spans over multiple
lines
*/Java is a statically-typed language, which means all variables must be declared before they can be used. Here are the eight primitive data types in Java:
-
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). -
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). -
int: The int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1. -
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -2^63 and a maximum value of 2^63-1. -
float: The float data type is a single-precision 32-bit IEEE 754 floating point. It is used to save memory in large arrays of floating point numbers. -
double: The double data type is a double-precision 64-bit IEEE 754 floating point. It is generally used for decimal values, such as currency. -
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. -
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
int myNumber = 88; // Integer (whole number)
double myDouble = 7.324; // Floating point number
char myLetter = 'A'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // StringNow that you've been introduced to the basics of Java syntax, try your hand at these exercises to reinforce your understanding:
-
Print Your Name: Write a Java program that prints your name to the console.
public class PrintName { public static void main(String[] args) { System.out.println("Your Name"); } }
-
Variable Practice: Declare one variable of each Java primitive type and print each variable to the console.
public class VariablePractice { public static void main(String[] args) { int myInt = 5; double myDouble = 5.99; char myChar = 'A'; boolean myBoolean = true; System.out.println("Integer: " + myInt); System.out.println("Double: " + myDouble); System.out.println("Character: " + myChar); System.out.println("Boolean: " + myBoolean); } }
-
Math Operations: Write a program that adds, subtracts, multiplies, and divides two numbers provided by the user and prints the results to the console.
import java.util.Scanner; public class MathOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first number: "); double firstNumber = scanner.nextDouble(); System.out.print("Enter second number: "); double secondNumber = scanner.nextDouble(); double sum = firstNumber + secondNumber; double difference = firstNumber - secondNumber; double product = firstNumber * secondNumber; double quotient = firstNumber / secondNumber; System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); System.out.println("Product: " + product); System.out.println("Quotient: " + quotient); } }
Remember to test your code for each exercise to ensure it works as expected. These exercises are designed to solidify your understanding of Java's basic syntax and data types.
Congratulations on completing the Easy Level: Basic Syntax and Concepts section! You're now ready to move on to more complex topics. Continue your learning journey with the next level of challenges.
Return to Home | Next: Medium Level - Code Reading and Analysis