Java Data Types ,keyword,Comments,
Data Types
In Java, data types specify the type of data that can be stored in a variable. Java provides two categories of data types: primitive data types and reference data types.
1. **Primitive Data Types**: Primitive data types represent single values and are not objects. They include:
- **Integral Types**:
- `byte`: 8-bit signed integer. Range: -128 to 127.
- `short`: 16-bit signed integer. Range: -32,768 to 32,767.
- `int`: 32-bit signed integer. Range: -2^31 to 2^31 - 1.
- `long`: 64-bit signed integer. Range: -2^63 to 2^63 - 1.
- **Floating-Point Types**:
- `float`: 32-bit floating-point number. Range: approximately ±3.40282347E+38F.
- `double`: 64-bit floating-point number. Range: approximately ±1.79769313486231570E+308.
- **Other Types**:
- `char`: 16-bit Unicode character. Range: '\u0000' to '\uffff'.
- `boolean`: Represents true or false.
2. **Reference Data Types**: Reference data types refer to objects and are stored on the heap. They include:
- **Class Types**: Object references to instances of classes.
- **Array Types**: Object references to arrays.
- **Interface Types**: Object references to instances of interfaces.
- **Enumeration Types**: Object references to instances of enum types.
Variables of reference data types store references to objects rather than the actual objects themselves.
Here's an example demonstrating the declaration and usage of different data types in Java:
program:-
class DataTypesExample {
public static void main(String[] args) {
// Primitive data types
byte byteVar = 100;
short shortVar = 10000;
int intVar = 100000;
// Note the 'L' suffix for long literals
long longVar = 10000000000L;
// Note the 'f' suffix for float literals
float floatVar = 3.14f;
double doubleVar = 3.14159;
char charVar = 'A';
boolean booleanVar = true;
// Reference data types
String stringVar = "Hello, Java!";
int[] arrayVar = {1, 2, 3, 4, 5};
MyClass myClassVar = new MyClass();
}
}
class MyClass {
// Class definition
}
``` In this example, `byteVar`, `shortVar`, `intVar`, `longVar`, `floatVar`, `doubleVar`, `charVar`, and `booleanVar` are variables of primitive data types, while `stringVar`, `arrayVar`, and `myClassVar` are variables of reference data types.
Java Keywords
Comments
code : 01
save filename--simple.java
// write a simple java program first save the file name (simple.java)
class simple
{
// main method
public static void main(String[] args)
{
System.out.println("\n\tWelcome Students");// print statement
}
}
output:-01
1.open run command---(windows+r)--enter cmd -- click ok
2.follow commands
current directly commands (cd)
java install path location address means C-drive(C:\\local disk)
c-drive enter folder name (lab)enter first compiler name of compiler code - javac space filename dot extension for java code - simple.java and enter
the second compiler name of interpreter code - java space program class name code - java simple and enter
code : 02
save filename--simple2.java /*
code 02
name : simple2.java
college : Nist
Roll no. : ------------
*/
class simple2
{
// main method
public static void main(String[] args)
{
System.out.println("\n\tWelcome Nist Students");// print statement
}
}
/* output:-
command prompt open follow commands
c:\Users\KISHORE> cd c:\lab
c:\lab> javac simple2.java
c:\lab> java simple2
Welcome Nist Students
c:\lab>
*/
output:-02
Java Escape Characters
code : 03
save filename--simple2.java
// Escape Characters
class EscapeCharacters
{
public static void main(String[] args)
{
System.out.println("1. New line");
System.out.println("welcome \nstudends");
System.out.println("2. Tab line");
System.out.println("welcome \tstudends");
System.out.println("3. Back space");
System.out.println("welcome \bstudends");
System.out.println("4. Carriage Return");
System.out.println("welcome \rstudends");
System.out.println("5. Double Quote");
System.out.println("welcome \"studends\"");
System.out.println("6. Single Quote");
System.out.println("welcome \'studends\'");
System.out.println("7. Back Slash");
System.out.println("welcome \\studends");
System.out.println("8.Unicode Escape");
System.out.println("welcome \u03A9 studends");
}
}
output:-02
Comments
Post a Comment