JAVA is one of the most popular programming languages all across the world. It is mainly used for developing a lot of application software. Since it is platform-independent, all this software can be executed on all devices.
However, JAVA has a lot of functions and methods that can make your task much simpler in the time of coding. So we brought you all the useful and important commands and features of JAVA that you must know.
- JAVA supports all sorts of arithmetic operations. To perform those, you have to simply use the addition (+), subtraction (–), division (/), or multiplication (*) operators if and when required.
- Relational operators such as “==, <,>, <=,>=,!=” are used for checking any sort of statement is true or false.
- To join any two statements, you can use the AND (&&) or OR (||) operators. These are mostly used for checking conditions simultaneously
- The postfix and prefix increment operators are used to increase or decrease the value assigned to the variable by one.
- JAVA has two types of Data Types: Primitive Data types and Non-primitive Data types.
- Primitive Data types are of eight types that include boolean, char, byte, short, int, long, float, and double. Each data type has a predefined size (in bits) that defines the size of the data it can store.
- Non-primitive Data types are of three types are Classes, Arrays, and Interfaces.
- Access modifiers in JAVA are of four types – default (no keyword), public, private, and protected. They are used to restrict the scope of any container like class, method, variable, etc.
- The static keyword in JAVA is used for memory management. The static keyword is used to create a constant variable or make the method the same for every instance of a class. The static keyword can be applied to blocks, variables, methods, or classes.
- Variables in JAVA are containers that can store a value based on the data type of the variable. For example, Integer (or int) variables can only store non-decimal numbers.
Syntax: <Access Modifier> <static/non-static> <Data type> <variable name> = <expression or value>
- Function or Method in JAVA is a block of code written to perform a specific function.
- The (.) operator is a very special operator present in JAVA. It is used to access all the methods of the class.
- The first step to start a JAVA program is by creating a class. The word class is a keyword used to create a class.
- If you want to declare an abstract class, you can use the abstract keyword before the class name.
- The import keyword is used to include certain predefined JAVA library packages into a program. JAVA packages have pre-written code for all types of simple to complex operations, including input and output operations.
- The try keyword is used to create a block of code in your program that can handle exceptions.
- Followed by the try keyword, the keyword catch is used to get hold of all the exceptions generated or thrown by the try.
- If you want to define a set of constants, you can use the enum keyword.
- Inheritance is one of the primary and key features of JAVA programming. The keyword extends used to signify inheritance.
- If you want to fix a value so that it cannot be changed, then the final keyword is the one that should be used.
- After the try-catch structure, the final keyword is used to execute the entire program.
- If you want to implement an interface, then the implements keyword is used for that task.
- The new keyword in JAVA is used to create new objects within the program.
- The super keyword in a program is used to refer to the parent class from which other classes are inherited.
- If you want to refer to a particular object in a function or a constructor, then this keyword is the one you should use.
- Just like the name suggests, the throw keyword is simply used to throw away an exception.
- If you want to declare an exception, then the throws keyword is used for doing this function.
- Output operations in JAVA can be performed using the “System.out.print()” command.
- Input operations in JAVA can be performed by importing specific JAVA packages like “java.io.*” or “java.util.scanner”.
- A parameter list of a function is a sequence of variables and their data types that are to be used in that function in JAVA.
- A return statement is for returning the value to the calling method if a return type is mentioned before the function or class.
- If there are two conditions to be checked from then in that scenario, the if-else statement is the perfect choice for coding. The if-else statement can be modified to check multiple conditions. It can be in the form of multiple-if, if else-if, etc.
- Another conditional operator in JAVA is the Ternary Operator which is basically the short form of the if-else statement. It can be written as: <variable> = (condition)? <true statement> : <false statement> When the condition is true, the value of the true statement gets assigned to the variable and vice versa.
- But if there are more than two conditions, then the switch statement is the ideal one. In the switch statement, the compiler only executes if a condition is true and the rest are left.
- If you want to terminate from a loop or any condition, then the break keyword is to be used; it acts as a termination statement. You can also use the exit() function to serve the same.
- If you want to create an entry control loop, then for that, you can use for loop and while loop structures. And if you want an exit control loop, the do-while loop is the ideal structure for the task.
- Arrays in JAVA are of two kinds: single-dimensional and multi-dimensional. An array is a container that can store multiple values of the same data type under a common variable name.
- A String in JAVA is a sequence of characters and is a non-primitive data type.
- If you want to convert an entire string into the lower case, then toLowerCase() is used
- For converting an entire string into the upper case, you can use the toUpperCase() function.
- If you want to replace any given character with another character, you can use the replace() function. Syntax: replace(‘a’, ‘b’) if you want to replace a by b.
- The trim() function can be used to remove all the white spaces from the beginning and end of a given string.
- equals() is a Boolean function to check if two given strings are the same or not. If you want to check the strings irrespective of the case, then you can use equalsIgnoreCase().
- If you want to check the length of the string given, you can use the length() function instead of extracting all the characters one by one.
- Suppose you want to find a character present in a particular position in a string. For that, you can use the CharAt() function. Syntax: CharAt(n) to find the character present in the nth position.
- If you want to compare two strings, then for that situation, the compareTo() function is present.
It provides a positive result if the 1st string is greater than the 2nd one.
It provides a negative result if 2nd string is greater than the first one
It provides zero as a result if both the strings are the same.
47. The concat() function is present to make the joining of two strings easier for the developer.
48. If you want to create a new string from an old string, you can use the substring() function.
Syntax: substring(n) is the syntax if you want to create a new string starting from the nth term of the string.
Substring(n,m) is used if you want to create a new string starting from the nth to the mth term.
49. To create the string representation of an object, the toString() function is used.
50. If you intend to find the position of a given character in a string, you can use the indexOf() function.
Syntax: indexOf(‘a’) will give the position of the first occurrence of the character a in the string.
Syntax: indexOf(‘a’, n) is to find the position of occurrence of ‘a’ after the nth index.
51. The ValueOf(variable) function is used to convert the parameter values to their string representations.
52. Comments in JAVA are parts of a code that is not executed by the compiler. It is simply used to provide details about different parts of the code and helps the programmer keep track of the code. It makes the code reader-friendly and debugging easier. Comments are of three types: single-line, multi-line, and documentation.