Classes in Java
Classes in Java
Printing methods
main() method
Data types
Wrapper Classes
Classes
Fields
Access Modifiers
Methods
Method Overloading
Class Modifiers
Let’s START …!!!
First Program
class HelloUPES{
public static void main(String[] args){
System.out.println("Hello UPES, How is the
Josh?");
}
}
UNKNOWNS…
Compile: javac HelloUPES.java
class
Execute: java HelloUPES.java public
static
Output: Hello UPES, How is the Josh? main
System.out.println . . . . . . . ???
Printing in Java Program
println() vs print()
class A{
public static void main(String[]
args){
System.out.println("Hello");
System.out.println("UPES");
System.out.print("Hello");
System.out.print("UPES");
}
}
Output: Hello
Alternate method: System.out.print() UPES
HelloUPES
main() method
class HelloUPES{
public static void main(String[] args){
System.out.println("Hello UPES, How is the
Josh?");
}
The
} program declares a class called HelloUPES with a single member: a method called
main().
The main() method of a class, if declared exactly as shown, is executed when you run the
class as an application. When run, a main() method can create objects, evaluate expressions,
invoke other methods, and do anything else needed to define an application’s behavior.
The main() method is declared public so that anyone can invoke it (in this case the Java
virtual machine).
The main() method is declared static, meaning that the method belongs to the class and is not
associated with a particular instance of the class. It can be invoked using the class name,
without creating an object of the class.
main() method
class HelloUPES{
public static void main(String[] args){
System.out.println("Hello UPES, How is the
Josh?");
}
}
The main() method is declared void because it doesn't return a value and so has no return
type.
The main() method’s only parameter is an array of String objects, referred to by the name
args. Arrays of objects are denoted by the square brackets [] that follow the type name.
In this example, the body of main() contains a single statement that invokes the println()
method and the semicolon ends the statement. A method is invoked by supplying an object
reference (in this case System.out the out field of the System class) and a method named
println() separated by a dot (.).
Data Types
The Java programming language has built-in “primitive” data types to support integer,
floating-point, boolean, and character values. The primitive data types are:
For each primitive type there is also a corresponding object type, generally termed a
“wrapper” class. For example, the class Integer is the wrapper class for int.
Wrapper Class
Java's primitive data types (int, char, boolean, double, etc.) are not objects, but in many
situations, you might need to treat them as objects, such as when working with collections,
using generics, or needing access to methods that are available only for objects. A wrapper
class in Java is a class that encapsulates a primitive data type, allowing it to be used as an
object.
Wrapper classes in Java allow you to treat primitive data types as objects by encapsulating
them in class instances. This allows you to access methods and additional functionality
provided by those wrapper classes. Primitive data types themselves do not have methods,
and you cannot directly call methods on them.
Wrapper Class
Here are the primitive data types and their corresponding wrapper classes:
byte → Byte
short → Short
int → Integer
long → Long
float → Float
double → Double
char → Character
boolean → Boolean
SIZE field in Wrapper Class
class _parse{
public static void main(String[] args){
String str = "45";
String str1 = "true";
int a = Integer.parseInt(str);
double d = Double.parseDouble(str); Output:
boolean b1 =
Boolean.parseBoolean(str1);
boolean b2 = 45 45.0 true
Boolean.parseBoolean(str); false
System.out.print(a+" "+d+" "+b1+"
"+b2);
}
}
Methods for Conversion
Integer.toString(int): Converts an int to a String.
Double.toString(double): Converts a double to a String.
Boolean.toString(boolean): Converts a boolean to a String.
class _tostring{
public static void main(String[] args)
{
Integer a = 100; Output:
Double d = 67.23;
Boolean b = true;
String str = a.toString();
100
System.out.println(str); 67.23
str = d.toString(); true
System.out.println(str);
str = b.toString();
System.out.print(str);
}
}
Methods for Formatting
Integer.toHexString(int): Returns the hexadecimal string representation of an int.
Integer.parseInt(String, int): Parses a string as a signed integer using the specified radix.
class radix{
public static void main(String[] args){ Output:
int a = 12;
String str = Integer.toHexString(a);
System.out.println(str); c
int b = Integer.parseInt(str, 16); 12
System.out.println(b); 10
System.out.println(Integer.parseInt("12",
8)); 12
System.out.println(Integer.parseInt("12",
10)); }
}
Class
Every object has a class that defines its data and behavior. In other words, Class is the
blueprint for objects in Java.
1. Fields are data variables associated with a class and its objects. Fields store results
of computations performed by the class.
2. Methods contain the executable code of a class. Methods are built from
statements. The way in which methods are invoked, and the statements contained
within those methods, are what ultimately directs program execution.
Within its own class a static field can be referred to directly, but when accessed externally it
must usually be accessed using the class name. For example, we could print the value of a
static field “nextID” within the class Body as follows:
System.out.println(Body.nextID);
A static member may also be accessed using a reference to an object of that class, such as:
System.out.println(mercury.nextID);
Static Field Example
class B{ class B{
int a = 0; static int a = 0;
void fun(){ void fun(){
a++; a++;
} }
} }
class A{ class A{
public static void main(String[] public static void main(String[]
arg){ arg){
B b1 = new B(); B b1 = new B();
b1.fun(); b1.fun();
B b2 = new B(); B b2 = new B();
b2.fun(); b2.fun();
B b3 = new B(); B b3 = new B();
b3.fun(); b3.fun();
class addition{
public int add(int a, int b){
return (a + b); }
public double add(double a, double b){
return (a + b); }
public int add(int a, int b, int c){
return (a + b * c); } Output:
}
class overloading{ 5
public static void main(String[] arg){
13.3
addition ad = new addition();
System.out.println(ad.add(2,3)); 14
System.out.println(ad.add(4.0,
9.3));
System.out.println(ad.add(2,3,4)); }
Some Important Points
static fields/variables can be created at class level only.
A static variable is allotted memory once.
static methods cannot use this or super keywords.
abstract methods cannot be static.
static methods cannot be overridden.
static methods can only access static variables and other static methods.
A static inner class cannot access instance variables and methods of the outer class without
the object’s reference.
A static inner class can access all static variables and methods of the outer class.
In Java, the outer class cannot be static.
Class Modifiers
A class declaration can be preceded by class modifiers that give the class certain properties:
public: A public class is publicly accessible: Anyone can declare references to objects of the
class or access its public members. Without a modifier a class is only accessible within its
own package.
abstract: An abstract class is considered incomplete and no instances of the class may be
created. Usually this is because the class contains abstract methods that must be
implemented by a subclass.
final: A final class cannot be subclassed.
• Wrapper class, size variable and some important methods (parsing, conversion, formatting)
• Class in Java