Which of the following are not Java Keywords
Java Keywords: In Java, a keywords is a reserved word that has a specific meaning in the programming language. These words are not allowed to be used as names for variables, methods, or classes.
In programming, a keyword is a word that is reserved by a programming language and has a specific meaning in that language. Keywords are an essential part of the syntax of a programming language and are used to represent specific operations or concepts.
Keywords are an important part of the structure of a programming language and are typically used to represent basic operations or concepts that are fundamental to the language. They are often used in combination with other syntax elements, such as variables, operators, and function calls, to create more complex programs.
|
Learn Best Java Keywords and Identifiers in Java |
Java Identifiers: In Java, an identifier is a name used to identify a class, method, variable, or other language element. There are a few rules to follow when choosing an identifier:
1. An identifier must start with a letter, a dollar sign ($), or an underscore (_).
2. After the first character, an identifier can contain any combination of letters, digits, dollar signs, and underscores.
3. An identifier cannot be a Java keyword or a reserved word.
4. An identifier cannot contain any spaces or special characters, except for the dollar sign and underscore.
What are the 53 reserved words in Java?
abstract | continue | for |
new | assert | default |
goto | package | synchronized |
boolean | do | if |
private | this | break |
double | implements | protected |
throw | byte | else |
import | public | throws |
case | enum | instanceof |
return | transient | catch |
extends | int | short |
try | char | final |
interface | static | void |
class | finally | long |
super | volatile | const |
float | native | while |
Abstract Keyword in Java
abstract keyword: The abstract keyword is used to define an abstract class or method. An abstract class is a class that cannot be instantiated and is meant to be a base class for one or more derived classes.
An abstract class can contain both abstract methods (methods without a body) and concrete methods (methods with a body). Abstract methods must be implemented by any non-abstract subclass.
abstract class Shape { // abstract method public abstract double area();
// concrete method public void draw() { System.out.println("Learn Coding Website"); } }
**********OUTPUT********** Learn Coding Website |
Continue Keyword in Java
continue keyword: The continue keyword is used to skip the current iteration of a loop and move on to the next iteration. It is used inside a looping structure, such as a for loop or a while loop.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }
**********OUTPUT********** 1 3 4 5 6 7 8 9 |
for Keyword in Java
for keyword: The for keyword is used to create a loop that iterates a specific number of times.
for (initialization; condition; increment) { statement(s); } |
for (int i = 1; i <= 10; i++) { System.out.println(i); }
**********OUTPUT********** 1 2 3 4 5 6 7 8 9 10 |
New Keyword in Java
new keyword: The new keyword is used to create an instance of a class. The new operator dynamically allocates memory for the object and calls the constructor of the class to initialize the object.
class MyClass { int x;
public MyClass() { x = 10; } }
...
MyClass myObj = new MyClass();
|
Assert Keyword in Java
assert keyword: The assert keyword is used to make a boolean test at runtime. It is used to test assumptions in a program and throw an AssertionError if the assumption is not true. The assert keyword is typically used for debugging purposes and is not meant to be used in production code.
Default Keyword in Java
default keyword: The default keyword is used in several contexts:
In a switch statement, the default keyword is used to specify a block of code that should be executed if none of the case labels match the value of the switch expression.
int x = 2;
switch (x) { case 1: System.out.println("x is 1"); break; case 2: System.out.println("x is 2"); break; default: System.out.println("x is not 1 or 2"); break; }
**********OUTPUT********** x is 2 |
goto Keyword in Java
goto keyword:The goto keyword is not used in Java. Java does not have a goto statement like some other programming languages.
Instead of using goto, Java provides other control statements such as break, continue, and return to enable flow control within a program.
Advantages of packages in java
package keyword: The package keyword is used to declare a package, which is a namespace that organizes a set of related classes and interfaces.
package com.example;
public class MyClass { // class code goes here } |
synchronized keyword in java
synchronized keyword: The synchronized keyword is used to provide mutually exclusive access to a block of code or method.
When a thread acquires the lock on an object and enters a synchronized block of code, no other thread can enter the block until the first thread exits the block. This ensures that only one thread can execute the code at a time, which can be useful for protecting shared resources or data.
public void increment() { synchronized (this) { count++; } } |
Boolean keyword in java
Boolean keyword: The boolean keyword is used to declare a variable as a boolean type, which can hold one of two values: true or false.
boolean flag = true; if (flag) { System.out.println("The flag is true!"); } else { System.out.println("The flag is false!"); } |
do Keyword in Java
do keyword: The do keyword is used to create a do-while loop.
A do-while loop is a loop that executes its body at least once, and then continues to execute as long as a given condition is true. The condition is checked at the end of each iteration, after the body of the loop has been executed.
int count = 10; do { System.out.println(count); count--; } while (count > 0); |
if Keyword in Java
if keyword: The if keyword is used to create an if statement, which allows a program to execute a block of code conditionally.
if (x > 10) { System.out.println("x is greater than 10"); } else { System.out.println("x is not greater than 10"); } |
Private Keyword in Java
private keyword: The private keyword is an access modifier that can be applied to class members (fields, methods, and nested classes) to indicate that they can only be accessed within the class or within inner classes.
public class MyClass { private int x;
// other class members go here } |
what is not the use of this keyword in java
this keyword: The this keyword refers to the current object of a class. It is used to access the members (fields and methods) of the current object from within the class.
public class MyClass { private int x;
public void setX(int x) { this.x = x; }
public int getX() { return this.x; } } |
Break Keyword in Java
break keyword: The break keyword is used to exit a loop or a switch statement.
When a break statement is encountered inside a loop, it causes the loop to terminate immediately and control to be transferred to the statement following the loop.
int x = 0; while (true) { x++; if (x > 10) { break; } System.out.println(x); } |
double Keyword in java
double keyword: double keyword is used to declare a variable of type double. A double is a 64-bit floating point data type that is used to represent decimal values. It is similar to the float data type, but has a greater range and precision.
double x = 1.5; double y = 2.5; double sum = x + y; |
Why we use implements keyword in java
implements keyword: implements keyword is used in a class declaration to specify that the class implements one or more interfaces. An interface is a set of related methods that a class can implement, but does not contain any implementation code. By implementing an interface, a class agrees to implement all the methods defined in the interface.
public class MyClass implements MyInterface { // class body goes here } |
What is the use of protected keyword in java
protected keyword: A protected keyword is a modifier that can be applied to methods, variables, and constructors. It has a different meaning depending on the context in which it is used.
When applied to a member of a class (such as a method, variable, or constructor), the protected keyword means that the member is visible within the class and any subclasses of the class, but is not visible to code outside the class or its subclasses.
public class MyClass { protected void myMethod() { // method body goes here } } |
throw Keyword in Java
throw keyword: The throw keyword is used to throw an exception. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
public void divide(int x, int y) { if (y == 0) { throw new ArithmeticException("Division by zero"); } int result = x / y; // code to use the result goes here } |
byte keyword in Java
byte keyword: The byte keyword is used to declare a variable of type byte. A byte is an 8-bit signed integer data type that has a range of -128 to 127. It is commonly used to represent small integer values, and it takes up less memory than the int data type.
byte x = 10; byte y = 20; byte sum = (byte)(x + y); // sum is now 30 |
else Keyword in Java
else keyword: The else keyword is used in an if-else statement to specify the code that should be executed if the condition in the if statement is false. The if-else statement is a control flow statement that allows you to execute different blocks of code based on a boolean condition.
if (x > 0) { // code to execute if x is greater than 0 } else { // code to execute if x is not greater than 0 } |
Use of import Keyword in Java
import keyword: The import keyword is used to import classes and interfaces from other packages. A package is a collection of related classes and interfaces that provide a namespace for the classes and interfaces it contains. The import keyword allows you to use classes and interfaces from other packages in your code without having to specify the full package name each time.
import java.util.Scanner; |
public Keyword in Java
public keyword: The public keyword is a modifier that can be applied to classes, methods, variables, and constructors. It indicates that the class, method, variable, or constructor is visible and can be accessed by any code.
public class MyClass { public void myMethod() { // method body goes here } } |
throws Keyword in Java
throws keyword: The throws keyword is used in a method declaration to indicate that the method may throw a specified exception or exceptions. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
public void myMethod() throws IOException { // method body goes here } |
case Keyword in Java
case keyword: The case keyword is used in a switch statement to specify a branch of code to be executed if the value of the switch expression matches the value of the case label. The switch statement is a control flow statement that allows you to execute different blocks of code based on a value.
int x = 2; switch (x) { case 1: // code to execute if x is 1 break; case 2: // code to execute if x is 2 break; case 3: // code to execute if x is 3 break; default: // code to execute if x is none of the above break; } |
enum Keyword in Java
enum keyword: The enum keyword is used to declare an enumerated type, which is a special data type that consists of a fixed set of values. An enumerated type is a type that has a fixed set of possible values, and each value is called an enumeration constant.
public enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } |
instanceof Keyword in Java
instanceof keyword: The instanceof keyword is a operator that is used to test whether an object is an instance of a particular class or interface. It returns true if the object is an instance of the specified class or interface, and false if it is not.
if (obj instanceof String) { // code to execute if obj is an instance of String } |
return Keyword in Java
return keyword: The return keyword is used in a method to exit the method and return a value to the caller. It is used to specify the value that should be returned by the method.
public void printSum(int x, int y) { int sum = x + y; System.out.println("The sum is: " + sum); return; } |
transient Keyword in Java
transient keyword: The transient keyword is a modifier that can be applied to a field to indicate that the field should not be serialized. Serialization is the process of converting an object's state into a byte stream, which can then be saved to a file or transmitted over a network. The transient keyword specifies that the field should not be included in the serialized form of the object.
public class MyClass { private transient int x; // other fields and methods go here } |
catch Keyword in Java
catch keyword: The catch keyword is used in a try-catch statement to specify a block of code that should be executed if an exception is thrown in the corresponding try block. The try-catch statement is a control flow statement that allows you to handle exceptions that may occur during the execution of your code.
try { // code that may throw an exception goes here } catch (IOException e) { // code to handle an IOException goes here } catch (SQLException e) { // code to handle an SQLException goes here } |
extends Keyword in Java
extends keyword: The extends keyword is used to indicate that a class is derived from another class, or that an interface is derived from another interface. This is known as inheritance, and it allows you to create a new class or interface that is a modified version of an existing class or interface.
public class DerivedClass extends BaseClass { // class body goes here } |
int Keyword in Java
int keyword: The int keyword is a primitive data type that represents an integer value. An integer is a whole number, either positive or negative, without a decimal point.
short Keyword in Java
short keyword: The short keyword is a primitive data type that represents a 16-bit signed integer value. A short is a whole number, either positive or negative, without a decimal point. It is smaller in size and range compared to the int data type, which is a 32-bit signed integer.
try Keyword in Java
try keyword: The try keyword is used in a try-catch statement to specify a block of code that should be executed and monitored for exceptions. The try-catch statement is a control flow statement that allows you to handle exceptions that may occur during the execution of your code.
try { // code that may throw an exception goes here } catch (Exception e) { // code to handle the exception goes here } |
char Keyword in Java
char keyword: The char keyword is a primitive data type that represents a single Unicode character. A char value is a 16-bit unsigned integer, which means it can represent any character in the Unicode character set, including letters, digits, and special characters.
Final Keyword in Java
final keyword: The final keyword is a modifier that can be applied to variables, methods, and classes to indicate that their value or behavior cannot be modified.
public static final int X = 23; |
keyword used for interface in java
interface Keyword: The interface keyword is used to define a set of related methods that a class must implement. An interface is essentially a blueprint for a class, specifying the methods that the class must implement but leaving the actual implementation of those methods up to the class.
public interface Shape { double getArea(); double getPerimeter(); } |
static keyword in java
static Keyword: Java programming language, the static keyword is used to indicate that a member (field or method) of a class belongs to the class itself, rather than to an instance of the class.
public class Counter { private static int count = 0; public static void increment() { count++; } public static int getCount() { return count; } } |
why we use void keyword in java
void Keyword: Java programming language, the void keyword is used to indicate that a method does not return a value. It is used as the return type of a method that does not return a value.
public void printMessage(String message) { System.out.println(message); } |
class keyword in java
class Keyword: In the Java programming language, the class keyword is used to declare a class. A class is a template for creating objects, and it defines a set of variables and methods that can be used by those objects.
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } } |
finally keyword in java
finally Keyword: Java programming language, the finally block is used in conjunction with the try and catch blocks to ensure that certain code is always executed, whether or not an exception is thrown.
try { // Code that might throw an exception goes here. } catch (Exception e) { // Code to handle the exception goes here. } finally { // Code that will always be executed goes here. } |
Long Keyword in Java
long Keyword: Java programming language, the long keyword is used to declare a variable of type long. The long data type is a 64-bit signed two's complement integer, which means that it can store integer values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
super keyword in java is used to mcq
super Keyword: Java programming language, the super keyword is used to refer to the parent class of an object or class. It is often used to access methods and fields of the parent class that have been overridden by the child class.
public class ChildClass extends ParentClass { public ChildClass() { super(); } @Override public void someMethod() { super.someMethod(); } } |
volatile keyword in java
volatile Keyword: Java programming language, the volatile keyword is used to indicate that a field is accessed by multiple threads, and that its value may be changed asynchronously by those threads.
public class MyClass { private volatile int x; public void setX(int x) { this.x = x; } public int getX() { return x; } } |
const keyword in java
const Keyword: The const keyword is not a reserved keyword and cannot be used. Instead, you can use the final keyword to declare a constant in Java.
public void myMethod(final int x) { final int y = x + 1; // x and y cannot be changed within the method. } |
float Keyword in Java
float Keyword: float keyword is used to declare a variable of type float. The float data type is a single-precision 32-bit floating-point type, which means that it can represent decimal values with a limited degree of precision.
native Keyword in Java
native Keyword: native keyword is used to declare a method that is implemented in native code, rather than in Java. Native code is code that is written in a language other than Java and that is compiled to run on a specific platform, such as C or C++ code on a Windows or Linux operating system.
MyClass obj = new MyClass(); obj.nativeMethod(); |
while keyword in java
while Keyword: while keyword is used to create a loop that executes a block of code repeatedly as long as a boolean condition is true.
int i = 0; while (i < 10) { System.out.println(i); i++; } |
FAQ Questions Java Keyword and Identifiers
Q.1 What are the 4 types of Java?
Ans:
1. Primitive types: These are the most basic data types and include boolean, char, byte, short, int, long, float, and double.
2. Reference types: These data types refer to objects and include strings, arrays, and classes.
3. Wrapper classes: These are classes that represent primitive data types. For example, the Integer class represents the int primitive type.
4. Void: This is a special type that represents the absence of a value or a return type. It is used as the return type for methods that do not return a value.
Q.5 What are the 7 features of Java?
Ans:
Q.2 What are the 4 OOP in Java?
Ans: Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of "objects", which can contain data and code that manipulate that data. OOP languages are designed to allow developers to create and work with objects in a natural and intuitive way.
Java is an object-oriented programming language, and as such, it includes several features that support OOP. The four main principles of OOP in Java are:
Encapsulation: This refers to the idea of bundling data and the methods that operate on that data within a single unit, or object. Encapsulation helps to reduce complexity and increase reusability by allowing objects to be self-contained and modular.
Inheritance: This allows a class to inherit properties and behavior from a parent class, which can help to reduce redundancy and improve code organization.
Polymorphism: This allows objects of different classes to be treated as a single type. This enables developers to write code that can work with multiple types of objects in a predictable and flexible way.
Abstraction: This refers to the idea of exposing only the necessary details of an object to the outside world, while hiding the implementation details. This helps to reduce complexity and improve modularity by allowing objects to be treated as black boxes.
Q.3 What are the 5 methods in Java?
Ans: Java, a method is a block of code that performs a specific task and may or may not return a value. Here are five common types of methods in Java:
1. void methods: These methods do not return a value. They are used to perform a specific task and may or may not take arguments as input.
2. static methods: These methods are associated with a class and not with a specific object. They can be called using the class name, without the need to create an object.
3. constructor methods: These methods are used to create and initialize an object. They have the same name as the class and are used to set the initial values of the object's attributes.
4. abstract methods: These methods are declared in an abstract class and do not have an implementation. They must be implemented by any concrete (non-abstract) subclass.
5. native methods: These methods are implemented in a language other than Java, such as C or C++. They are used to access system-specific functionality that is not available in Java.
Q.4 What are the 12 features of Java?
Ans: Java is a popular programming language that is widely used for building a variety of applications, including web, mobile, and enterprise applications. Here are 12 features of the Java language that contribute to its popularity and usefulness:
1. Object-Oriented: Java is an object-oriented programming language, which means that it is based on the concept of "objects" that contain both data and code that manipulate that data. This allows developers to create reusable and modular code.
2. Platform-Independent: Java programs are compiled into a machine-independent bytecode that can run on any device that has a Java Virtual Machine (JVM) installed. This makes it easy to write code that can run on multiple platforms, such as Windows, Mac, Linux, and others.
3. High-Level Language: Java is a high-level programming language, which means that it is easy for humans to read and write. This makes it a good choice for developers who want to focus on solving business problems rather than low-level details of the underlying hardware.
4. Simple: Java has a simple syntax that is easy to learn, which makes it a good choice for beginners. It also has a large standard library that provides a wide range of built-in functions and classes, which reduces the need to write boilerplate code.
5. Robust: Java has a strong type system and a garbage collector that helps to prevent common programming errors, such as null pointer exceptions and memory leaks. It also has a large and active developer community that helps to identify and fix bugs.
6. Secure: Java was designed with security in mind and includes features such as a classloader, a bytecode verifier, and a security manager that help to prevent security vulnerabilities.
7. Multithreaded: Java supports the creation of multiple threads of execution within a single program, which can help to improve the performance of applications that perform multiple tasks concurrently.
8. Dynamic: Java includes a number of features that allow it to adapt to changing environments at runtime, such as reflection and the ability to load new classes dynamically.
9. Portable: Java programs can be easily moved from one platform to another, which makes it a good choice for developers who want to write code that can run on multiple platforms.
10. Scalable: Java is designed to support the development of large-scale applications, with features such as garbage collection and a high-performance virtual machine that help to ensure good performance and scalability.
11.Strongly Typed: Java is a strongly typed language, which means that variables and expressions have a fixed type that cannot be changed at runtime. This helps to prevent common programming errors and makes the code easier to understand.
12. Modern: Java is a modern programming language that is constantly evolving and adding new features. It is widely used in a variety of applications, including web, mobile, and enterprise applications, and has a large and active developer community.
0 Comments