Jav

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

JAVA

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine


because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed. It can also run those programs
which are written in other languages and compiled to Java bytecode.

VM, JRE, and JDK are platform dependent because the configuration of each OS is
different from each other. However, Java is platform independent.

JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software tools which are used for developing
Java applications. It is used to provide the runtime environment. It is the
implementation of JVM. It physically exists. It contains a set of libraries + other files
that JVM uses at runtime.

JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications and
applets. It physically exists. It contains JRE + development tools.

The JDK contains a private Java Virtual Machine (JVM) and a few other resources
such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java
Application.

An applet is a program written in the Java programming language that can be included
in an HTML page,
 A set of classes and interfaces grouped together are known as Packages in JAVA.
 ex of package is import java.util.*;
 util package in Java is a built-in package that contains various utility classes and
interfaces.
 ex in util package are arrayS, ARRAY LIST ETC
 BOOLEAN-1Bit, char-16bits, int-32bits, long-64,float,32,double-64bits,
1byte=8bits
 primitve data types- can store only one value,pre-defined. includes
int,boolean,char etc.
 non-primitve- can store multiple values, user-defined, includes array etc.

A class is a user-defined blueprint or prototype from which objects are created. Using
classes, you can create multiple objects with the same behavior instead of writing
their code multiple times.
 Objects are instance of class

https://www.w3schools.com/java/java_methods.asp

 An instance variable is a variable that is specific to a certain object. It is declared


within the curly braces of the class but outside of any method.
Eg : public String name its public instance

Class Names
i. The first letter of the class should be in Uppercase (lowercase is allowed but
discouraged).

ii. If several words are used to form the name of the class, each inner word ’ s first
letter should be in Uppercase. Underscores are allowed, but not recommended. Also
allowed are numbers and currency symbols, although the latter are also discouraged
because they are used for a special purpose (for inner and anonymous classes).

class MyJavaProgram // valid syntax


class 1Program // invalid syntax

Method Names
i. All the method names should start with a lowercase letter (uppercase is also allowed
but lowercase is recommended).

ii. If several words are used to form the name of the method, then each first letter of
the inner word should be in Uppercase. Underscores are allowed, but not
recommended. Also allowed are digits and currency symbols.

public void employeeRecords() // valid syntax


public void EmployeeRecords() // valid syntax, but discouraged

Certainly! Let's break down each part of the Java code snippet public static void
main(String[] args):

1. *public*: This is an access modifier, which indicates that the method main is
accessible from anywhere. In Java, public means that the method can be accessed by
any other class.

2. *static*: This keyword allows the method main to be called without needing to
create an instance of the class. In Java, the main method is typically declared as static
because it serves as the entry point for the program and can be invoked without
creating an object of the class containing it.

3. *void*: This is the return type of the method. void indicates that the main method
does not return any value. In Java, the main method does not need to return anything
because it is simply the starting point of the program.

4. *main*: This is the name of the method. In Java, the main method is the entry
point of a Java application. It is where the execution of the program begins.

5. *String[] args*: This is the parameter of the main method. It is an array of strings
(String[]) named args. When the Java program is executed from the command line,
any command-line arguments provided are passed to the main method through this
parameter. The args parameter allows the program to receive input or configuration
from the command line.

1. Interpreter: An interpreter is a program that directly executes instructions written


in a high-level programming language without the need for prior compilation. It
translates the source code into machine code or intermediate code line by line and
executes it immediately. Interpreters are commonly used in scripting languages like
Python, JavaScript, and Ruby. They provide advantages such as ease of debugging
and portability across different platforms.

2. *Compiler: A compiler is a software tool that translates the entire source code
written in a high-level programming language into machine code or an intermediate
code called object code. Unlike interpreters, compilers generate the complete
executable file before execution. This executable file can be run multiple times
without needing to recompile the source code each time. Commonly used compiled
languages include C, C++, and Java. Compilers offer advantages such as faster
execution speed and optimization of code.

3. *Debugger: A debugger is a software tool used by developers to identify and


correct errors or bugs in a program. It allows developers to inspect the state of a
program during its execution, set breakpoints to pause the program at specific points,
examine variables, and step through the code line by line to trace the flow of
execution. Debuggers are invaluable in the software development process for
diagnosing and fixing issues in programs.

Identifiers in java
Identifiers are the names of local variables, instance and class variables, and labels,
but also the names for classes, packages, modules and methods. All Unicode
characters are valid, not just the ASCII subset.

i. All identifiers can begin with a letter, a currency symbol or an underscore (_).
According to the convention, a letter should be lower case for variables.

ii. The first character of identifiers can be followed by any combination of letters,
digits, currency symbols and the underscore. The underscore is not recommended for
the names of variables. Constants (static final attributes and enums) should be in all
Uppercase letters.

iii. Most importantly identifiers are case-sensitive.

iv. A keyword cannot be used as an identifier since it is a reserved word and has some
special meaning.

Legal identifiers: MinNumber, total, ak74, hello_world, $amount, _under_value


Illegal identifiers: 74ak, -amount

Java Keywords
Keywords or Reserved words are the words in a language that are used for some
internal process or represent some predefined actions. These words are therefore not
allowed to use as variable names or objects.

eg: int, abstract, char, String etc.

Access Modifiers
The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class. We can change the access level of fields, constructors, methods,
and class by applying the access modifier on it.

There are four types of Java access modifiers:

Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
Default: The access level of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will be
the default.
Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.

Variables
Variables are containers for storing data values. In Java, there are different types of
variables, for example: String - stores text, such as "Hello". String values are
surrounded by double quotes. int - stores integers (whole numbers), without decimals,
such as 123 or -123.
A data type is a classification that specifies which type of value a variable can hold or
which operations can be performed on that variable.

Local Variables
A variable defined within a block or method or constructor is called a local variable.

Static variables are also known as class variables.


These variables are declared similarly to instance variables. The difference is that
static variables are declared using the static keyword within a class outside of any
method, constructor, or block.
Unlike instance variables, we can only have one copy of a static variable per class,
irrespective of how many objects we create.

Scope:
Scope defines the region of the program where a variable can be accessed.
In Java, variables have block scope, meaning they are accessible only within the block
in which they are declared.
For instance, a variable declared within a method is only accessible within that
method.
Variables declared within a block (e.g., within loops or conditional statements) are
only accessible within that block.
Lifetime:
Lifetime refers to the duration for which a variable remains in memory.

Public means it can be accesses anywhere. Static means its belongs to particular
method etc.

Loops Concept
In Java, while, do-while, and for loops are used for repetitive execution of a block of
code. While they serve a similar purpose, they have different syntaxes and are suitable
for different scenarios:

1. *While Loop*:
- Syntax: while (condition) { // code block }
- The while loop executes a block of code repeatedly as long as the specified
condition is true.
- It first evaluates the condition, and if it's true, executes the code block. Then, it
re-evaluates the condition. If the condition is still true, the loop continues, and so on.
The loop terminates when the condition becomes false.
- The condition is tested before entering the loop, so there's a possibility that the
code inside the loop may never execute if the condition is initially false.
- Example:
java
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}

2. *Do-While Loop*:
- Syntax: do { // code block } while (condition);
- The do-while loop is similar to the while loop, but it guarantees that the code
block is executed at least once, regardless of whether the condition is true or false.
- It first executes the code block and then evaluates the condition. If the condition
is true, the loop continues; otherwise, it terminates.
- Example:
java
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);

3. *For Loop*:
- Syntax: for (initialization; condition; update) { // code block }
- The for loop provides a concise way to iterate over a range of values or to iterate
based on a counter.
- It consists of three parts: initialization (executed once before the loop starts),
condition (evaluated before each iteration; if false, the loop terminates), and update
(executed after each iteration).
- The initialization, condition, and update parts are separated by semicolons.
- Example:
java
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
In summary, while and do-while loops are used when the number of iterations is not
known beforehand or when you want to repeat a block of code until a certain
condition is met, while for loops are used when the number of iterations is known or
when you want to iterate over a range of values.

Difference Between Function and Method


Functions Methods
Do not have reference variables. Are called by reference variable.
It does not have access control, i.e., it can be declared and defined anywhere in the
code. It contains access control, therefore only within the class should it be declared
and defined.
Both object-oriented and non-object-oriented languages are compatible with the
Function. Only object-oriented programming languages can use methods.
It is called individually and by its name. It is called using the name or reference of
its object.
As it is called individually, the data is passed externally or explicitly. As it is called
dependently, the data is passed internally or implicitly.

 A reference variable is a variable that points to an object of a given class


 The main aim of OOP is to bind together the data and the functions that operate
on them so that no other part of the code can access this data except that function.
 Lists are mutable that is they can be changed after created but tuples are
immutable, they cannot be changed
 A copy constructor in a Java class is a constructor that creates an object using
another object of the same Java class
 malloc() is a function that creates one block of memory of a fixed size. calloc() is
a function that assigns a specified number of blocks of memory to a single
variable.
 If a method is overloaded, the compiler decides which method is to be executed
by verifying the argument list and return type at compile time. So this is "compile
time polymorphism".
 If there are overridden methods, which method is to be executed depends on the
type of object ; that is decided at RUNTIME by the JVM.
 Compile time polymorphism in Java is also known as early binding or static
polymorphism. The binding is performed during compilation time and hence the
name compile-time polymorphism. Binding refers to connecting the function call
to the function body.
 Compile-time is the time at which the source code is converted into an executable
code while the run time is the time at which the executable code is started
running.
 The destructor is the opposite of the constructor. The constructor is used to
initialize objects while the destructor is used to delete or destroy the object that
releases the resource occupied by the object.
 Remember that there is no concept of destructor in Java. In place of the destructor,
Java provides the garbage collector that works the same as the destructor.
It is a special method that automatically gets called when an object is no longer
used. When an object completes its life-cycle the garbage collector deletes that
object and deallocates or releases the memory occupied by the object.
 time complexity is relation bw input size and running time(operations). Time
complexity of an algorithm quantifies the amount of time taken by an algorithm
to run as a function of the length of the input.
 best case . omega(1), avg case is theta(n+1)/2. worst case is O(n)
 While calling a function, we pass the values of variables to it. Such functions are
known as “Call By Values”. While calling a function, instead of passing the
values of variables, we pass the address of variables(location of variables) to the
function known as “Call By References.
 A data structure is a specialized format for organizing, processing, retrieving and
storing data. The four basic data structure types are linear data structures, tree
data structures, hash data structures and graph data structures.
 linear data strcutures- arrays,queues,linked lists ,stacks.
 Length applies to a Java array, and size applies to a Java ArrayList collection
object.
 A linked list in Java is a dynamic data structure whose size increases as you add
the elements and decreases as you remove the elements from the list.
 Bit manipulation is the process of applying logical operations on a sequence of
bits, the smallest form of data in a computer, to achieve a required result.

Prime number :
import java.util.Scanner;
public class CodesCracker
{
public static void main(String[] args)
{
int num, i, count=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number: ");
num = s.nextInt();

for(i=2; i<num; i++)


{
if(num%i == 0)
{
count++;
break;
}
}

if(count==0)
System.out.println("\nIt is a Prime Number.");
else
System.out.println("\nIt is not a Prime Number.");
}
}
Fibonacci:
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}
}

// Java program to determine whether the number is Armstrong number or not


public class ArmstrongNumber {

public static void main(String[] args)


{
int n = 153;
int temp = n;
int p = 0;

// Function to calculate the sum of individual digits

while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}

// Condition to check whether the value of P equals to user input or not.

if (temp == p) {
System.out.println("Yes. It is Armstrong No.");
Reverse a string
import java.util.*;
public class yoo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char ch=0;
String nchar="";
for(int i=0;i<str.length();i++){

ch=str.charAt(i);
nchar=ch+nchar;

}
System.out.println(nchar);
}

Factorial
import java.util.*;
public class yoo {
public static int fact(int n){
if(n==0){
return 1;
}
else{
return n*fact(n-1);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(fact(n));
}

}
Occurence_of_a_string
import java.util.*;
public class yoo {
public static void main(String[] args) {
String str="ssidda";
int len;
int counter[]=new int[256];
for(int i=0;i<str.length();i++){
counter[(int)str.charAt(i)]++;
}
for(int i=0;i<256;i++){
if(counter[i]!=0){
System.out.println((char)i+"-->"+counter[i]);
}
}
}

 Arrays doesn't have Size they're used for arraylist etc.


 In static memory allocation, once the memory is allocated, the memory size can
not change. In dynamic memory allocation, when memory is allocated the
memory size can be changed.
 Software development life cycle (SDLC) is a structured process that is used to
design, develop, and test good-quality software.It ensures that the end product is
able to meet the customer’s expectations and fits within the overall budget.

The 7 Phases Of SDLC (Software Development Life Cycle)


Stage 1: Project Planning
Project Planning | First Step In SDLC | BetsolThe first stage of SDLC is all about
“What do we want?” Project planning is a vital role in the software delivery lifecycle
since this is the part where the team estimates the cost and defines the requirements of
the new software.

Stage 2: Gathering Requirements & Analysis


Gathering Requirement and Analysis | Second Step In SDLC | BetsolThe second step
of SDLC is gathering maximum information from the client requirements for the
product. Discuss each detail and specification of the product with the customer. The
development team will then analyze the requirements keeping the design and code of
the software in mind. Further, investigating the validity and possibility of
incorporating these requirements into the software system. The main goal of this stage
is that everyone understands even the minute detail of the requirement. Hardware,
operating systems, programming, and security are to name the few requirements.

Stage 3: Design
Design | Third Step In SDLC | BetsolIn the design phase (3rd step of SDLC), the
program developer scrutinizes whether the prepared software suffices all the
requirements of the end-user. Additionally, if the project is feasible for the customer
technologically, practically, and financially. Once the developer decides on the best
design approach, he then selects the program languages like Oracle, Java, etc., that
will suit the software.

Once the design specification is prepared, all the stakeholders will review this plan
and provide their feedback and suggestions. It is absolutely mandatory to collect and
incorporate stakeholder’s input in the document, as a small mistake can lead to cost
overrun.
Stage 4: Coding or Implementation
Coding or Implementation | Fourth Step In SDLC | BetsolTime to code! It means
translating the design to a computer-legible language. In this fourth stage of SDLC,
the tasks are divided into modules or units and assigned to various developers. The
developers will then start building the entire system by writing code using the
programming languages they chose. This stage is considered to be one of the longest
in SDLC. The developers need certain predefined coding guidelines, and
programming tools like interpreters, compilers, debugger to implement the code.

The developers can show the work done to the business analysts in case if any
modifications or enhancements required.

Stage 5: Testing
Testing | Fifth Step In SDLC | BetsolOnce the developers build the software, then it is
deployed in the testing environment. Then the testing team tests the functionality of
the entire system. In this fifth phase of SDLC, the testing is done to ensure that the
entire application works according to the customer requirements.

After testing, the QA and testing team might find some bugs or defects and
communicate the same with the developers. The development team then fixes the
bugs and send it to QA for a re-test. This process goes on until the software is stable,
bug-free and working according to the business requirements of that system.

Stage 6: Deployment
Development | Sixth Step In SDLC | BetsolThe sixth phase of SDLC: Once the testing
is done, and the product is ready for deployment, it is released for customers to use.
The size of the project determines the complexity of the deployment. The users are
then provided with the training or documentation that will help them to operate the
software. Again, a small round of testing is performed on production to ensure
environmental issues or any impact of the new release.

Stage 7: Maintenance
Maintenance | Seventh Step In SDLC | BetsolThe actual problem starts when the
customer actually starts using the developed system and those needs to be solved from
time to time. Maintenance is the seventh phase of SDLC where the developed product
is taken care of. According to the changing user end environment or technology, the
software is updated timely.

https://docs.google.com/document/d/1OrUsnDqzma9ob467aCdvRlZwkK7mhl552n92
vlBXr_U/edit

You might also like