A data type is a classification of data based on the type of value it holds. In Java, data types specify the size and type of values that can be stored in a variable.
Here are some examples of data types in Java:
- Integer (Int)
int
– This data type is used to store integer values. For example:
int myAge = 30;
Floating-point Numbers (double)
double
– This data type is used to store decimal values. For example:
double myWeight = 70.5;
- Boolean (boolean)
boolean
– This data type is used to store true
or false
values. For example:
boolean isStudent = true;
- Text (char)
Text (Char)char
– This data type is used to store a single character. For example:
char myGrade = 'A';
- Text (String)
String
– This data type is used to store a sequence of characters. For example:
String myName = "John";
- Floating-point Numbers (float)
float
– This data type is used to store decimal values with single precision. For example:
float myHeight = 1.75f;
- Integer (long)
long
– This data type is used to store integer values with a larger range than int. For example:
long mySalary = 10000000000L;
These are just a few examples of data types in Java. Different data types have different size limitations and are used to store different types of values.
Print Numbers and Texts
To print numbers and text in Java, you can use the System.out.print()
or System.out.println()
method. This method is used to display text on the console output.
Here’s an example of how to print a number and text using this method:
int num = 10;
String text = "Hello, World!";
System.out.print("The number is: " + num);
System.out.println("The text is: " + text);
Differnce between print() and println()
Feature | print() | println() |
---|---|---|
Outputs text | Yes | Yes |
Adds a newline character | No | Yes |
Continues printing on the same line | Yes | No |
Syntax | print("text") | println("text") |
Example | print("Hello"); print("World") | println("Hello"); println("World") |
Output | HelloWorld |
|