UNIT - 1

You might also like

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

UNIT - 1

1. Bytecode is a highly optimized set of instructions that is executed by the Java


Virtual Machine. Bytecode helps Java achieve both portability and security. It
helps in achieving platform-independence which is one of the reasons why
James Gosling started the formation of Java. The set of instructions for a JVM(
Java Virtual Machine) may differ from system to system but can all interpret
Bytecode. Bytecodes are non-runnable codes that rely on the availability of an
interpreter, this is where JVM comes into play.

Program:
Import java.lang.*;
Import java.util.*;
Import java.io.*;
class PrintfDemo
{
public static void main(String[] args)
{
String s="sai";
int n=65;
float f=15.45f;
System.out.printf(" String =%s %n number=%d %n HexaDecimal=%x %n
Float=%f",s,n,n,f);
}
}

2. Type conversion:

Type conversion in Java is the process of converting one data type to another data type. This is
often necessary when performing operations on data of different types or when assigning a
value of one type to a variable of another type. There are two types of type conversion in Java:

Implicit Type Conversion (also known as widening conversion)


● This occurs automatically when a value of a smaller data type is assigned to a variable
of a larger data type.
● For example, assigning an int value to a long variable:
Int i=10;
Long l=i;

Explicit Type Conversion (also known as narrowing conversion)


● This requires a type cast operator and is used when a value of a larger data type is
assigned to a variable of a smaller data type.
● For example, casting a long value to an int variable:
Long l=1234567890L;
Int i=(int) l;

Program:

Import java.lang.*;
Import java.io.*;
Import java.util.*;
class Test
{
public static void main(String[] args)
{
int i = 200;
// automatic type conversion
long l = i;
// automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}

Output:
Int value 200
Long value 200
Float value 200.0

Type casting:
In Java, type casting is the process of converting one data type into another data type.
There are two types of casting:
1. Implicit Casting (Widening Casting)
● This occurs when you convert a smaller data type to a larger data type.
For example, converting an int to a double.
● This can be done automatically by the compiler, as there is no data loss
involved.
2. Explicit Casting (Narrowing Casting)
● This occurs when you convert a larger data type to a smaller data type.
For example, converting a double to an int.
● This must be done manually, as data loss may occur during the
conversion.
● To perform explicit casting, you can use parentheses and the target data
type, like so:

3. Buzz words:

Platform Independence: Java works on different platforms (Windows, Mac, Linux,


Raspberry Pi, etc.) and Programs developed in JAVA are executed anywhere on any
System.

• Simple: They made java simple by eliminating difficult concepts of C and C++.
example the concept of pointers and Java is simple because it has the same syntax of C
and C++

• Object Oriented: Object oriented throughout - no coding outside of class definitions,


including main ()

• Compiler/Interpreter Combo: Usually a computer language is either compiled or


interpreted. Java combines both these approaches thus making java a two-stage
system.First, java compiler translates source code into bytecode. Bytecodes are not
machine instructions and therefore, in the second stage, java interpreter generates
machine code that can be directly executed by the machine that is running the java
program so we can say that java is both compiled and interpreted language.

• Robust: because of the following concept included in java it is called as robust


1. Exception handling
2. strong type checking
3. local variables must be initialized.

• Automatic Memory Management: Automatic garbage collection - memory


management handled by JVM.

Tokens:
A token is the smallest element of a program that is meaningful to the compiler. Tokens
can be
classified as follows:
• Keywords
• Identifiers
• Constants
• Special Symbols
• Operators
1. Keyword: Keywords are pre-defined or reserved words in a programming language.
Each keyword is meant to perform a specific function in a program. Since keywords
are referred names for a compiler, they can’t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword which is not
allowed. Java language supports following keywords:
abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

enum

exports
extends

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

module

native

new

open

opens

package

private

protected

provides

public

requires

return

short

static

strictfp

super
switch

synchronized

this

throw

throws

to

transient

transitive

try

uses

void

volatile

while

with

2. Identifiers: Identifiers used for naming classes, methods, variables, objects, labels
and
interfaces in a program.
Java identifiers follow the following rules:
1. Identifier must start with a letter, a currency character ($), or a connecting
character such as underscore (_).
2. Identifier cannot start with a number.
3. Uppercase and lowercase letters are distinct.
4. Total, TOTAL and total are different.
5. They can be of any length.
6. It should not be a keyword.
7. Spaces are not allowed

3. Constants: Constants are also like normal variables. But the only difference is, their
values cannot be modified by the program once they are defined. Constants refer to
fixed values. They are also called as literals. Constants may belong to any of the data
type.
Syntax: final data_type variable_name;
4. Special Symbols: The following special symbols are used in Java having some
special meaning and thus, cannot be used for some other purpose.
[] () {}, ; * =
• Brackets[]: Opening and closing brackets are used as array element reference.
These indicate single and multidimensional subscripts.
• Parentheses(): These special symbols are used to indicate function calls and
function parameters.
• Braces{}: These opening and ending curly braces marks the start and end of a
block of code containing more than one executable statement.
• comma (, ): It is used to separate more than one statements like for separating
parameters in function calls.
• semi colon : It is an operator that essentially invokes something called an
initialization list.
• asterick (*): It is used to create pointer variable.
• assignment operator: It is used to assign values.
5. Operators: Java provides many types of operators which can be used according to
the
need. They are classified based on the functionality they provide. Some of the types
are-
• Arithmetic Operators
• Unary Operators
• Assignment Operator
• Relational Operators
• Logical Operators
• Ternary Operator(conditional operator)
• Bitwise Operators and bit-shift operators
• instance of operator

4. Responsibilities:

1. Loading: The JVM is responsible for loading the necessary class files that are
required to run a Java program. It searches for the required class files in the
classpath, which is a list of directories and JAR files that contain the class files.
2. Verifying: Before executing the bytecode, the JVM verifies the class files to
ensure that they are valid and do not violate any security constraints. This
process includes checking the syntax, semantics, and bytecode instructions of
the class files.
3. Interpreting or JIT Compiling: The JVM interprets the bytecode instructions or
dynamically compiles them into machine code. The interpretation and
compilation process is optimized by the Just-In-Time (JIT) compiler, which
compiles frequently used code to improve performance.
4. Memory Management: The JVM manages the memory allocation and
deallocation for Java programs. It provides automatic memory management
through garbage collection, which frees up memory by automatically deleting
objects that are no longer needed.
5. Security: The JVM provides a secure environment for executing Java programs. It
uses a sandbox security model that restricts the access of Java programs to
system resources, such as the file system, network, and hardware.
6. Exception Handling: The JVM provides a mechanism for handling exceptions that
occur during program execution. It catches and handles exceptions according to
the rules of the Java programming language.

5. https://dotnettutorials.net/lesson/operators-in-java/(operators answer)

6. Static methods and variables include the keyword static before their name in the header or
declaration. They can be public or private.
● Static variables belong to the class, with all objects of a class sharing a single static variable.
● Static methods are associated with the class, not objects of the class.
● Static variables are used with the class name and the dot operator, since they are associated
with a class, not objects of a class.
● Static methods cannot access or change the values of instance variables, but they can
access or change the values of static variables.
● Static methods cannot call non-static methods.
Program:
Import java.lang.*;
Import java.io.*;
Import java.util.*;
public class Person
{
// instance variables
private String name;
private String email;
private String phoneNumber;

// Static counter variable


public static int personCounter = 0;

// static method to print out counter


public static void printPersonCounter() {
System.out.println("Person counter: " + personCounter);
}

You might also like