0% found this document useful (0 votes)
22 views36 pages

Class As User Defined Type - Theory

The document provides an overview of data types in programming, distinguishing between primitive and composite data types, with a focus on user-defined types such as classes. It explains the process of defining classes, creating objects, and accessing class members, along with the concept of wrapper classes for primitive data types. Additionally, it covers methods for converting between data types and includes examples of character and integer wrapper classes.

Uploaded by

Parshveer Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views36 pages

Class As User Defined Type - Theory

The document provides an overview of data types in programming, distinguishing between primitive and composite data types, with a focus on user-defined types such as classes. It explains the process of defining classes, creating objects, and accessing class members, along with the concept of wrapper classes for primitive data types. Additionally, it covers methods for converting between data types and includes examples of character and integer wrapper classes.

Uploaded by

Parshveer Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

ROOPESH SAIGAONKAR

CLASS AS USER
DEFINED TYPE

ROOPESH SAIGAONKAR

1
DATATYPES

▰ The type of value that can be stored in a given variable is known as


datatype.
▰ Significance of giving datatype to a variable
a) The type of value that can be stored in the variable
b) The type of operation that can be performed on the variable

ROOPESH SAIGAONKAR

2 2
ROOPESH SAIGAONKAR

CATEGORIES OF
DATATYPES

These datatypes exist on their


own. They are also known as
Datatype

Primitive
Built-in datatypes

These datatypes depend on


primitive datatype. They are also
Composite known as User Defined Types
value or User Defined Datatypes
(UDT)

3 3
ROOPESH SAIGAONKAR

DIFFERENCE BETWEEN PRIMITIVE AND


COMPOSITE DATATYPE

Primitive Datatypes Composite Datatypes


These are the built in These datatypes are defined
datatypes by the users. They are also
known as user defined
datatypes
They exists on their own. They are based on primitive
datatypes
They have fix memory size They don’t have fix memory
size.
e.g. char, int, double e.g. String, class, object

4 4
ROOPESH SAIGAONKAR

CLASS AS USER DEFINED TYPE


(COMPOSITE DATATYPE)
• Class consist of variables and objects of different
datatypes and it is created by the user. So it is a
composite datatype.
• Class is collection of variables and related methods.
To use these variables and methods we need to
create object of the given class. From one class
multiple objects of similar kind can be created, so
class is known factory of objects.
5 5
ROOPESH SAIGAONKAR

DEFINING A CLASS
• Syntax:
access_specifier keyword class classname
{
datatype variables; // instance variables
void method()
{
datatype variables; // local variables
statement 1;
statement 2;
.
.
.
statement n;
}
} 6 6
ROOPESH SAIGAONKAR

EXAMPLE
public static class program1
{
int a; // instance variables
public static void main()
{
int b=10; // local variables
a=5;
int c = a+b;
System.out.println(“sum =“+c);
}
}
7 7
ROOPESH SAIGAONKAR

OBJECTS

• Objects are software bundles of variables and related methods


• It is an instance of class.
• They are used refer/access the members of the class i.e. using objects
one can access variables and methods of the given class
• The process of creating objects of the class using new keyword is known
as Instantiation

8 8
ROOPESH SAIGAONKAR

INSTANTIATION/OBJECT
CREATION

• Syntax: (Non Static Class)


classname objectname = new classname();
e.g.: Scanner sc = new Scanner(System.in);
Scanner à Class name
sc à Object Name
new à keyword to create objects. It also allocates memory to
the objects/variable
Scanner() à Constructor name. constructor is special method
which has the name same as the class
name
9 9
ROOPESH SAIGAONKAR

SECTION A QUESTION

• Write java statement to create a object mp4 for the class


digital

digital mp4 = new digital();

10 10
ROOPESH SAIGAONKAR

ACCESSING THE MEMBERS OF


THE STATIC CLASS
• dot '.' operator is used to access the member of class. Member of the
class are variables (instance variables) and Methods/Functions

• Syntax: (Static Class)


classname.membername;
e.g.:
Math.pow(2,3);

Math à Class name (Math Library class

. à operator to access the member of the class


pow() à member function of Math library class
11 11
ROOPESH SAIGAONKAR

ACCESSING THE MEMBERS OF


THE NON STATIC CLASS

.
• dot ' ' operator is used to access the member of class. Member of the class are variables
(instance variables) and Methods/Functions

• Syntax: (Non Static Class)


objectname.membername; // sc.nextLine();
e.g.:
class abc
{
int a;
public static void main()
{
abc obj = new abc(); // object creation
obj.a=10; // accessing member of the class
System.out.println(obj.a);
}
}
12 12
ROOPESH SAIGAONKAR

WRAPPER CLASS

▰ Wrapper class wraps the datatype(value) and gives it the appearance of an


object.
▰ It contains various functions to convert the value of one data type to
another datatype and function to manipulate the given values.
▰ Wrapper class are static class( We don’t need to create object for wrapper
class)
▰ All primitive datatypes have wrapper class
▰ Using wrapper class we convert value of
v Primitive datatype to another primitive datatype
v Primitive datatype to Composite datatype and
vice – a – versa

13 13
ROOPESH SAIGAONKAR

WRAPPER CLASS

▰Remember all wrapper class are static class so


we don’t need any object to access the member
functions of this class.

To access the function:


Classname.functionname();

14 14
ROOPESH SAIGAONKAR

WRAPPER CLASS
Datatype Wrapper class
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean
15 15
ROOPESH SAIGAONKAR

DIFFERENCE BETWEEN AUTOBOXING AND


UNBOXING

Autoboxing / Boxing Unboxing


It is a process of converting a It is a process of converting
primitive value to the reference type to primitive
reference type value
Implicit Conversion Explicit Conversion
Eg.: Eg.:
int k = 15; Integer n = new Integer(15);
Integer n = new Integer(k); int k = (int)n;

16 16
ROOPESH SAIGAONKAR

17 17
ROOPESH SAIGAONKAR

CHARACTER WRAPPER
CLASS

▰ Character is a wrapper class for the char datatype.


▰ It mainly contains functions which are used to manipulate the character
values

Syntax:
Character.function_name(char argument);

18 18
ROOPESH SAIGAONKAR

CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLetter() boolean ans = Checks whether the given
Character.isLetter(ch1); argument is a
alphabet(letter) or not. It
will return answer either
true or false. In the
example ans=true
isDigit() boolean a = Character.isDigit(ch1); Checks whether the given
boolean b = Character.isDigit(ch2); argument is a Digit or not.
It will return answer either
true or false. In the
example
a=false
b=true

19 19
ROOPESH SAIGAONKAR

CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLetterOrDigit boolean a = Checks whether the
() Character.isLetterOrDigit(ch1); given argument is a
alphabet/Digit or
not. It will return
answer either true or
false. In the example
a=true
isWhitespace() boolean a = Character.isWhitespace(ch1); Checks whether the
given argument is a
blank space or not. It
will return answer
either true or false.
In the example
a=false
20 20
ROOPESH SAIGAONKAR

CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='9';
isLowerCase() boolean a = Character.isLowerCase(ch1); Checks whether the
given argument is in
Small letter/ Lower
case or not. It will
return answer either
true or false. In the
example a=false
isUpperCase() boolean a = Character.isUpperCase(ch1); Checks whether the
given argument is in
Capital letter/ Upper
case or not. It will
return answer either
true or false. In the
example a=true
21 21
ROOPESH SAIGAONKAR

CHARACTER WRAPPER
CLASS
Function Usage Description
name char ch1 = 'M'; char ch2='d';
toLowerCase() char a = Character.toLowerCase(ch1); Converts the given
argument to Small
letter/ Lower case. It
will return answer in
char datatype. In the
example
a='m'
toUpperCase() char a = Character.toUpperCase(ch1); Converts the given
char b = Character.toUpperCase(ch2); argument to Capital
letter/ Upper case. It
will return answer in
char datatype. In the
example
a=‘M‘
b=‘D' 22 22
ROOPESH SAIGAONKAR
WAP TO INPUT A CHARACTER. CHECK AND PRINT WHETHER
THE CHARACTER VALUE IS A LETTER, A DIGIT OR SPECIAL
CHARACTER. ALSO CHECK WHETHER IT IS IN LOWER CASE
OR UPPER CASE
import java.util.*;
class Program111
{
public static void main()
{ else if(Character.isDigit(ch)==true)
Scanner sc=new Scanner(System.in); {
System.out.println("Enter Any value"); System.out.println("It is Number");
char ch=sc.next().charAt(0); }
if(Character.isLetter(ch)==true) else
{ {
if(Character.isUpperCase(ch)==true) System.out.println("It is Special
{ character");
System.out.println("entered value is }
Capital Letter"); } // end of main method
else } // end of class
{
System.out.println("entered value is
Small Letter");
} 23 23

}
ROOPESH SAIGAONKAR
WAP TO INPUT A LETTER. PRINT THE LETTER IN TOGGLE
CASE I.E. IF THE CHARACTER IS IN UPPER CASE THEN PRINT
IN LOWER CASE AND VICE A VERSA

import java.util.*;
class Program112
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Any value"); else
char ch=sc.next().charAt(0); {
if(Character.isLetter(ch)==true) System.out.println(“Not an
{
if(Character.isUpperCase(ch)==true)
alphabet");
{ }
} // end of main method
System.out.println(Character.toLowerCase(ch)); } // end of class
}
else
{

System.out.println(Character.toUpperCase(ch));
} 24 24
}
ROOPESH SAIGAONKAR

INTEGER WRAPPER
CLASS

▰ parseInt() à This function helps to convert value of string datatype to


integer datatype
e.g. :
String s="475";
String p="125";
String a=s+p; // a="475125"
int n = Integer.parseInt(s); //n=475
int m = Integer.parseInt(p); //m=125
int b = m+n; //b=600

25 25
ROOPESH SAIGAONKAR
ROOPESH SAIGAONKAR

INTEGER WRAPPER
CLASS

▰ valueOf() à This function helps to convert value of string datatype to


integer object
e.g. :
String s="475";
String p="125";
String a=s+p; // a="475125"
int n = Integer.valueOf(s); //n=475
int m = Integer.valueOf(p); //m=125
int b = m+n; //b=600

26 26
ROOPESH SAIGAONKAR

INTEGER WRAPPER
CLASS

▰ toString() à This function helps to convert value of integer datatype


to string datatype
e.g. :
int n=475;
String s = Integer.toString(n); // s="475"

27 27
ROOPESH SAIGAONKAR

LONG WRAPPER CLASS

▰ parseLong() à This function helps to convert value of string datatype


to long datatype
e.g. :
String s="475";
long n = Long.parseLong(s); // n=475

28 28
ROOPESH SAIGAONKAR

LONG WRAPPER CLASS

▰ valueOf() à This function helps to convert value of string datatype to


long object
e.g. :
String s="475";
long n = Long.valueOf(s); // n=475

29 29
LONG WRAPPER CLASS

▰ toString() à This function helps to convert value of long datatype to


string datatype
e.g. :
long n=475L;
String s = Long.toString(n); // s="475"

30 30
ROOPESH SAIGAONKAR

DOUBLE WRAPPER
CLASS

▰ parseDouble() à This function helps to convert value of string


datatype to Double datatype
e.g. :
String s="475";
double n = Double.parseDouble(s); // n=475.0

31 31
ROOPESH SAIGAONKAR

DOUBLE WRAPPER
CLASS

▰ valueOf() à This function helps to convert value of string datatype to


Double object
e.g. :
String s="475";
double n = Double.valueOf(s); // n=475.0

32 32
ROOPESH SAIGAONKAR

DOUBLE WRAPPER
CLASS

▰ toString() à This function helps to convert value of double datatype


to string datatype
e.g. :
double n=475; // n = 475.0
String s = Double.toString(n); // s="475.0"

33 33
ROOPESH SAIGAONKAR

FLOAT WRAPPER CLASS

▰ parseFloat() à This function helps to convert value of string


datatype to float datatype
e.g. :
String s="475";
float n = Float.parseFloat(s); // n=475.0f

34 34
FLOAT WRAPPER CLASS

▰ valueOf() à This function helps to convert value of string datatype to


float object
e.g. :
String s="475";
float n = Float.valueOf(s); // n=475.0f

35 35
ROOPESH SAIGAONKAR

FLOAT WRAPPER CLASS

▰ toString() à This function helps to convert value of float datatype to


string datatype
e.g. :
float n=475f; // n = 475.0f
String s = Float.toString(n); // s="475.0"

36 36

You might also like