Sunday, April 27, 2025

Java Tutorial - Data Types

Data types define the type of data that can be stored in a variable, the size of the variable, and the range of values that it can hold.  

import java.util.Date;

public class Main {

	public static void main(String[] args) {
		int age = -50;
		String someString = "Some Value";
		char firstLetter = 'C';
		
		long largeNum = 2_000_000_000L;
		float voltage = 13.8F;
		
		Date rightNow = new Date();
		
		System.out.println(age);				
		System.out.println(someString);
		System.out.println(firstLetter);
		System.out.println(largeNum);
		System.out.println(voltage);
		
		System.out.println(rightNow);		
		
	}	
}

Here's an explanation of the code line by line: 

import java.util.Date;

This line imports the Date class from the java.util package. The Date class provides methods for working with dates and times in Java.

public class Main {

This line declares a public class named Main. The class contains a main method that serves as the entry point for the program. 

public static void main(String[] args) {

This line declares the main method, which is executed when the program is run. The args parameter is an array of strings that can be used to pass command-line arguments to the program. 

int age = -50;
String someString = "Some Value";
char firstLetter = 'C';

These lines declare and initialize three variables. age is an integer variable that is initialized to -50. someString is a string variable that is initialized to "Some Value". firstLetter is a character variable that is initialized to the character 'C'. 

long largeNum = 2_000_000_000L;
float voltage = 13.8F;

These lines declare and initialize two more variables. largeNum is a long integer variable that is initialized to 2 billion (2,000,000,000). The L at the end of the value indicates that it is a long integer. voltage is a floating-point variable that is initialized to 13.8. The F at the end of the value indicates that it is a float. 

Date rightNow = new Date();

This line creates a new instance of the Date class and assigns it to the rightNow variable. 

System.out.println(age);
System.out.println(someString);
System.out.println(firstLetter);
System.out.println(largeNum);
System.out.println(voltage);

These lines print the values of the variables to the console using the System.out.println() method. 

System.out.println(rightNow);

This line prints the current date and time, represented by the Date object, to the console.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...