Basics
Basic syntax and functions from the Java programming language.
Boilerplate
Showing Output
It will print something to the output console.
Taking Input
It will take string input from the user
It will take integer input from the user
It will take float input from the user
It will take double input from the user
Primitive Type Variables
The eight primitives defined in Java are int, byte, short, long, float, double, boolean, and char those aren't considered objects and represent raw values.
byte
byte is a primitive data type it only takes up 8 bits of memory.
long
long is another primitive data type related to integers. long takes up 64 bits of memory.
float
We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. Which means if we get past six decimal points, this number becomes less precise and more of an estimate.
char
Char is a 16-bit integer representing a Unicode-encoded character.
int
int holds a wide range of non-fractional number values.
short
If we want to save memory and byte is too small, we can use short.
Comments
A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.
Single line comment
Multi-line comment
Constants
Constants are like a variable, except that their value never changes during program execution.
Arithmetic Expressions
These are the collection of literals and arithmetic operators.
Addition
It can be used to add two numbers
Subtraction
It can be used to subtract two numbers
Multiplication
It can be used to multiply add two numbers
Division
It can be used to divide two numbers
Modulo Remainder
It returns the remainder of the two numbers after division
Augmented Operators
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.
Tab
It gives a tab space
Backslash
It adds a backslash
Single quote
It adds a single quotation mark
Question mark
It adds a question mark
Carriage return
Inserts a carriage return in the text at this point.
Double quote
It adds a double quotation mark
Type Casting
Type Casting is a process of converting one data type into another
Widening Type Casting
It means converting a lower data type into a higher
Narrowing Type Casting
It means converting a higher data type into a lower
Decision Control Statements
Conditional statements are used to perform operations based on some condition.
if Statement
if-else Statement
if else-if Statement
Ternary Operator
It is shorthand of an if-else statement.
Syntax
Example
Switch Statements
It allows a variable to be tested for equality against a list of values (cases).
Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the coder.
while Loop
It iterates the block of code as long as a specified condition is True
for Loop
for loop is used to run a block of code several times
for-each Loop
do-while Loop
It is an exit controlled loop. It is very similar to the while loop with one difference, i.e., the body of the do-while loop is executed at least once even if the condition is False
Break statement
break keyword inside the loop is used to terminate the loop
Continue statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop
Arrays
Arrays are used to store multiple values in a single variable
Declaring an array
Declaration of an array
Defining an array
Defining an array
Accessing an array
Accessing the elements of an array
Changing an element
Changing any element in an array
Array length
It gives the length of the array
Loop through an array
It allows us to iterate through each array element
Multi-dimensional Arrays
Arrays can be 1-D, 2-D or multi-dimensional.
};
Methods
Methods are used to divide an extensive program into smaller pieces. It can be called multiple times to provide reusability to the program.
Declaration
Declaration of a method
Calling a method
Calling a method
Example
Method Overloading
Method overloading means having multiple methods with the same name, but different parameters.
Recursion
Recursion is when a function calls a copy of itself to work on a minor problem. And the function that calls itself is known as the Recursive function.
Strings
It is a collection of characters surrounded by double quotes.
Creating String Variable
String Length
Returns the length of the string
String Methods toUpperCase()
Convert the string into uppercase
toLowerCase()
Convert the string into lowercase
indexOf()
Returns the index of specified character from the string
concat()
Used to concatenate two strings
Math Class
Math class allows you to perform mathematical operations.
Methods max() method
It is used to find the greater number among the two
min() method
It is used to find the smaller number among the two
sqrt() method
It returns the square root of the supplied value
random() method
It is used to generate random numbers
Object-Oriented Programming
It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities.
class
A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
object of class
An object is an instance of a Class.
Encapsulation
Encapsulation is a mechanism of wrapping the data and code acting on the data together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class.
Inheritance
Inheritance can be defined as the process where one class acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
Example
Polymorphism
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
File Operations
File handling refers to reading or writing data from files. Java provides some functions that allow us to manipulate data in the files.
Assume that we have created the file “D:\\Example.txt”
canRead method
Checks whether the file is readable or not
createNewFile method
It creates an empty file
canWrite method
Checks whether the file is writable or not
exists method
Checks whether the file exists
delete method
It deletes a file
getName method
It returns the name of the file
getAbsolutePath method
It returns the absolute pathname of the file
length Method
It returns the size of the file in bytes
list Method
It returns an array of the files in the directory
mkdir method
It is used to create a new directory
close method
It is used to close the file
To write something in the file
Exception Handling
An exception is an unusual condition that results in an interruption in the flow of the program.
try-catch block
try statement allow you to define a block of code to be tested for errors. catch block is used to handle the exception.
Example
finally block
finally code is executed whether an exception is handled or not.
Comments
Post a Comment