0% found this document useful (0 votes)
1 views4 pages

Java Keywords and Examples

The document provides an overview of various programming concepts and data types in Java, including Boolean, Break, Class, Character, Float, Loop, Object, Return, Scanner, and Static. Each concept is defined with a brief explanation and an example code snippet. This serves as a quick reference for understanding basic Java programming elements.

Uploaded by

jaitakeda27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Java Keywords and Examples

The document provides an overview of various programming concepts and data types in Java, including Boolean, Break, Class, Character, Float, Loop, Object, Return, Scanner, and Static. Each concept is defined with a brief explanation and an example code snippet. This serves as a quick reference for understanding basic Java programming elements.

Uploaded by

jaitakeda27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Boolean

 It is a data type that can only store true or false values.


 Example:

Boolean b = new Boolean(boolean value);

Break

 It breaks out of a loop or a switch block.


 Example:

Class Test {

Public static void main(String[] args) {

// for loop

For (int i = 1; i <= 10; ++i) {

// if the value of i is 5 the loop terminates

If (i == 5) {

Break;

System.out.println(i);

}
}

Class

 This defines a class


 Example:

Public class Main {


Int x = 5;

}
Character (char)

 It is a data type that is used to store a single character.


 Example:

Char myVar1 = 65, myVar2 = 66, myVar3 = 67;

System.out.println(myVar1);

System.out.println(myVar2);

System.out.println(myVar3);

Float

 It is a data type that can store fractional numbers from 3.4e−038 to 3.4e+038.
 Example:

Float myNum = 5.75f;

System.out.println(myNum);

Loop

 A loop is a programming structure that allows a block of code to be executed


repeatedly, either a specific number of times or until a certain condition is met.
 Example:

For (int i = 0; i < 5; i++) {

System.out.println(i);
}

Object

 An object is a specific instance of a class. It’s a real, concrete entity created from
a class’s blueprint, possessing its own unique data and able to perform actions
defined by its class.
 Example:

Public class Main {

Int x = 5;

Public static void main(String[] args) {


Main myObj = new Main();

System.out.println(myObj.x);

Return

 This finishes the execution of a method, and can be used to return a value from a
method.
 Example:

 Example public class Main {

 static int myMethod(int x) {

 return 5 + x;

 }

 public static void main(String[] args) {


System.out.println(myMethod(3));

 }

 }

 // Outputs 8 (5 + 3)

Scanner

 Scanner is a Java class (a pre-built tool) that makes it easy to read input from
various sources, most commonly from the user’s keyboard in console
applications.

Example:

Import java.util.Scanner;

Static

 It is a non-access modifier used for methods and attributes. Static


methods/attributes can be accessed without creating an object of a class.
 Example:
Import java.util.Scanner; // Import the Scanner class

Class Main {

Public static void main(String[] args) {

Scanner myObj = new Scanner(System.in); // Create a Scanner object

System.out.println(“Enter username”);

String userName = myObj.nextLine(); // Read user input


System.out.println(“Username is: “ + userName); // Output user input

You might also like