Data Types in Java | Master DSA in Java

TShaped Skills
29 Aug 202428:43

Summary

TLDRThis video script offers an in-depth exploration of Java's data types, emphasizing the importance of understanding them in a strongly typed language. It covers primitive data types such as byte, short, int, long, float, double, boolean, and char, explaining their memory allocation and range limitations. The script also touches on non-primitive data types like arrays and strings, which are treated as objects in Java. The default data types for integers and real numbers are highlighted, along with the mandatory specification of data types in Java, a practice crucial for clear and error-free programming.

Takeaways

  • πŸ˜€ Java is a strongly typed programming language, meaning data types must be explicitly specified when working with data.
  • πŸ”’ The script introduces Java's primitive data types, including byte, short, int, long, float, double, boolean, and char, each with specific memory allocation and use cases.
  • πŸ“ The memory allocation for storing data types in Java varies: byte (1 byte), short (2 bytes), int (4 bytes), and long (8 bytes), affecting how numbers are stored and manipulated.
  • πŸ”‘ The range of values that can be stored with each data type is defined by its memory size, with byte having the smallest range and long the largest.
  • 🎯 When a specific data type is not mentioned for a whole number in Java, the default data type is int.
  • πŸ“‰ For real numbers with fractional parts, Java offers the float and double data types, with float requiring an 'f' suffix and double being the default without a suffix.
  • πŸ“ The memory allocation for float is 4 bytes and for double is 8 bytes, reflecting their capacity for precision.
  • πŸ›‘ Boolean data type in Java can only store two values: true or false, and the memory allocation for boolean is JVM-specific with no fixed byte size.
  • πŸ‘οΈ The char data type is used to store a single character, with 2 bytes of memory allocated per character.
  • πŸ“š The script emphasizes the importance of understanding data types for memory optimization and proper data handling in Java.
  • πŸ”„ The video also mentions non-primitive data types like arrays and strings, which are considered objects in Java, and will be discussed in more detail in future lessons.

Q & A

  • Why is understanding data types crucial when working with Java?

    -Understanding data types is crucial in Java because it is a strongly typed programming language, which requires specifying the type of data being worked with. This ensures the correct allocation of memory and the appropriate range of values for variables.

  • What are the primitive data types supported by Java?

    -Java supports several primitive data types including byte, short, int, long, float, double, boolean, and char. These types allow direct storage of values in memory.

  • Why are there different types for storing integer values in Java, such as byte, short, int, and long?

    -Different types for integer values allow for memory optimization and accommodate different ranges of numbers. The choice among byte, short, int, and long depends on the size of the number to be stored, with each type occupying a different amount of memory and allowing a specific range of values.

  • What is the significance of the 'L' suffix when declaring a long variable in Java?

    -The 'L' suffix is used to denote a long literal in Java. It is mandatory when declaring a long variable to avoid confusion with an int, especially when the literal exceeds the range of int.

  • What is the default data type for a whole number in Java if no type is specified?

    -In Java, if no data type is specified for a whole number, the default data type is int.

  • How does Java handle the result of an operation on whole numbers?

    -In Java, the result of an operation on whole numbers is always an integer, regardless of the original data types of the operands, unless explicitly cast to a smaller data type like byte or short.

  • What are the two values that can be stored using the Boolean data type in Java?

    -The Boolean data type in Java can store only two values: true and false.

  • What is the purpose of the char data type in Java?

    -The char data type in Java is used to store a single character, including alphabets, numbers, and special characters, within single quotes.

  • Why is the memory allocation for the Boolean data type in Java considered JVM-specific and not fixed?

    -The memory allocation for the Boolean data type in Java is JVM-specific because the Java Virtual Machine may optimize storage for Boolean values differently depending on the implementation, and there is no fixed documentation for the exact number of bytes used.

  • What is the default data type for a real number in Java if no type is specified?

    -In Java, if no data type is specified for a real number, the default data type is double.

  • What suffix is required when declaring a float literal in Java?

    -When declaring a float literal in Java, an 'f' or 'F' suffix is required to differentiate it from a double literal.

  • How many bytes of memory are typically allocated for storing a char value in Java?

    -In Java, storing a char value typically occupies two bytes of memory.

Outlines

00:00

πŸ“˜ Introduction to Java Data Types

The script begins with an introduction to Java as a strongly typed language, emphasizing the importance of understanding data types. It outlines the necessity to specify data types when working with data and introduces the concept of primitive data types in Java. The instructor promises a detailed discussion on data types in a dedicated Java playlist and starts explaining the primitive data types such as byte, short, int, long, float, double, boolean, and char. It also mentions non-primitive data types like arrays and strings, which will be discussed separately, and clarifies that these are considered objects in Java, not primitive data types.

05:01

πŸ”’ Exploring Integer Data Types in Java

This paragraph delves into the integer data types available in Java: byte, short, int, and long. It explains the memory allocation for each type, with byte using 1 byte, short using 2 bytes, int using 4 bytes, and long using 8 bytes of memory. The script also covers the range of values each data type can store, such as -128 to 127 for byte, -32,768 to 32,767 for short, and so on. The instructor discusses the significance of choosing the right data type based on the range of values needed and demonstrates how the Java compiler provides errors for values outside the specified range.

10:01

πŸ”‘ Default Data Types and Memory Allocation

The script explains the default data types in Java, highlighting that the default data type for whole numbers is int. It also discusses the importance of specifying data types to avoid compilation errors and the automatic memory allocation based on the data type chosen. The instructor provides examples in Eclipse IDE to illustrate how the compiler behaves with and without data type suffixes like 'L' for long and 'F' for float, and how the range of storable values affects the choice of data type.

15:02

πŸ“‰ Real Numbers and Boolean Data Types

The focus shifts to real numbers and the data types float and double, which are used to store numbers with fractional parts. The script explains the need for suffixes 'F' for float and no suffix for double, and the memory allocation differences between them, with float using 4 bytes and double using 8 bytes. It also touches on the Boolean data type, which can only store true or false values, and mentions that the memory occupied by Boolean is JVM-specific and not fixed.

20:03

πŸ†Ž Character Data Type and Its Memory Usage

The script introduces the character data type, char, which is used to store single characters such as letters, digits, or special characters. It clarifies that only one character can be stored per char variable and that two bytes of memory are occupied for each char. The instructor also discusses the importance of using single quotes for character literals and the mandatory nature of this syntax in Java.

25:05

πŸ“š Summary of Data Types and Future Discussions

In the concluding paragraph, the script summarizes the importance of understanding data types in Java for effective programming. It reiterates the use of int for whole numbers, double or float for real numbers, Boolean for true/false values, and char for characters. The instructor also previews upcoming discussions on collections of data, arrays, strings, and other non-primitive data types in the Java series. The script ends with an invitation to subscribe to the channel and engage with the content by liking and sharing the video.

Mindmap

Keywords

πŸ’‘Strongly Typed

In programming, 'strongly typed' refers to a language that requires explicit declarations of variable types during the coding process. This is a fundamental concept in the video, as it sets the stage for the importance of understanding data types in Java. The script emphasizes that Java, being strongly typed, necessitates the specification of data types for every variable, which is crucial for the program's execution and memory allocation.

πŸ’‘Primitive Data Types

Primitive data types are the basic data types that are provided by a programming language to store values without any additional information. In the video, the instructor discusses various primitive data types in Java, such as byte, short, int, long, float, double, boolean, and char. These types are essential for understanding how data is stored and manipulated in Java, as they directly allocate memory and define the range of values that can be stored.

πŸ’‘Byte

A 'byte' is a primitive data type in Java that occupies one byte of memory and is used to store small whole numbers within a specific range. The video script uses 'byte' as an example to explain the concept of memory allocation and range limitations among data types, noting that it can store values from -128 to 127.

πŸ’‘Integer

An 'integer' is a whole number without any fractional or decimal part. In the context of the video, the term is used to describe the type of data that can be stored using the int, byte, short, and long data types in Java. The script explains that integers are commonly used in programming for storing values like age or scores, which are whole numbers.

πŸ’‘Memory Allocation

Memory allocation refers to the process of reserving a certain amount of memory for storing data. The video script discusses how different data types in Java, such as byte, short, int, and long, allocate different amounts of memory (1, 2, 4, and 8 bytes respectively), which affects the range of values they can store and the efficiency of the program.

πŸ’‘Range

In the context of data types, 'range' refers to the set of values that a particular type can hold. The video script explains the range of values for different primitive data types, such as the byte type having a range of -128 to 127, and the importance of choosing the right data type based on the range needed for the variable's value.

πŸ’‘Float and Double

Both 'float' and 'double' are primitive data types in Java used for storing real numbers, which include fractional parts. The video script distinguishes between the two by explaining that 'float' occupies 4 bytes of memory and requires an 'f' suffix, while 'double' occupies 8 bytes and does not require a suffix. The choice between them depends on the precision required for the variable's value.

πŸ’‘Boolean

A 'Boolean' is a primitive data type that can only store two values: true or false. In the video, the Boolean type is introduced as a way to represent truth values in Java, which is essential for conditional statements and logical operations within a program.

πŸ’‘Char

The 'char' data type is a single 16-bit Unicode character in Java. The video script explains that 'char' is used to store any single character, emphasizing that it must be enclosed in single quotes and occupies 2 bytes of memory. It is fundamental for handling character-based data in Java applications.

πŸ’‘Non-Primitive Data Types

Non-primitive data types, also known as reference types, are data types that are not primitive and are treated as objects in Java. The video script mentions arrays and strings as examples of non-primitive data types. They are discussed separately from primitive data types because they do not store values directly but rather store references to objects in memory.

Highlights

Java is a strongly typed language, requiring explicit data type specification for variables.

Understanding data types is crucial for working with data in Java.

Java supports primitive data types such as byte, short, int, long, float, double, boolean, and char.

Non-primitive data types in Java include arrays and strings, which are considered objects.

The choice between data types depends on memory allocation and the range of values that need to be stored.

Byte data type allocates one byte of memory and is suitable for small integer values within the range of -128 to 127.

Short data type uses two bytes of memory and can store values ranging from -32,768 to 32,767.

Int is the default data type for integers in Java, allocating four bytes of memory with a range from -2^31 to 2^31-1.

Long data type requires eight bytes of memory and has a significantly larger range of storable values.

Floating-point numbers can be stored using float or double, with float requiring four bytes and double eight bytes.

Boolean data type can only store true or false and does not have a fixed memory size.

Char data type is used for storing characters and allocates two bytes of memory per character.

Java provides automatic type conversion for smaller ranges to larger ranges, but not vice versa.

When a data type is not specified for a whole number in Java, the default type is int.

For real numbers without a specified type, Java defaults to double.

Suffixes like 'L' for long and 'F' for float are mandatory to avoid implicit type conversions.

The video provides a comprehensive introduction to data types in Java, suitable for beginners in the language.

Transcripts

play00:00

hello and welcome back in the last video

play00:02

we came to one conclusion that Java is

play00:05

strongly typed programming language

play00:07

hence understanding data types is very

play00:10

crucial here whenever we are going to

play00:12

work with any data or information we may

play00:15

have to specify what type of data we are

play00:17

working on hence data types become very

play00:21

important by keeping in mind whatever is

play00:23

required to understand or to go ahead in

play00:26

our course I will be discussing the

play00:28

fundamentals of data types again there

play00:30

can be a detailed discussion on data

play00:32

types as well which I'm going to have in

play00:35

my Java playlist for now to proceed

play00:37

further let me talk on the primitive

play00:40

data types with Java supports to start

play00:43

with we have a primitive data type by

play00:45

the name bite and then we have short we

play00:49

have integer primitive data type what

play00:51

they are how to work with them I'm going

play00:54

to you know speak I'm going to talk on

play00:56

that now then we have long followed by

play01:00

float then we have double and also we

play01:05

have Boolean alongside that we also have

play01:08

care data type all of these are the

play01:11

primitive data types using them we can

play01:14

directly store the value in the memory

play01:17

alongside that we do also have a

play01:19

non-primitive data type we have arrays

play01:22

and string which going forward

play01:24

separately we are going to have

play01:26

discussion on array and string is not

play01:29

primitive data type in Java they are

play01:31

considered as object when it comes to

play01:34

primitive whatever I have listed here

play01:36

only those are the primitive data type

play01:39

now whenever you want to store any of

play01:42

the integer value any of the whole

play01:44

number then you can use bite short int

play01:48

long so whenever we have a whole number

play01:51

whole number means a number without any

play01:53

decimal point without any fractional

play01:55

point for an instance we have a data of

play01:58

45 or maybe we have data 5545 any range

play02:03

we have any of these data types can be

play02:06

used what is the significance why we

play02:08

have all four different options I'm

play02:10

going to talk that so whenever we have a

play02:12

whole number which you want to store in

play02:15

a variable in your program in your Java

play02:18

application then you can use either bite

play02:20

short int long when to use bite when to

play02:24

you short why we have four options all

play02:27

these things we will try to discuss here

play02:29

so so whenever we have integer type of

play02:32

value a whole

play02:33

number then we can go with either of

play02:37

these data types whenever we have a real

play02:40

number whenever we have a real number

play02:43

then we can go with any of these data

play02:46

type float or double why we have float

play02:48

why we have double we will understand

play02:50

separately in this particular video but

play02:52

to start with if you have a real number

play02:54

with a fractional point maybe I have

play02:58

45.5 54 4.6 whenever we have this

play03:02

decimal point then you can go with

play03:03

either float or double whenever we have

play03:06

something called as true or false to be

play03:08

stored a Boolean data true or false then

play03:11

you can go with Boolean data type again

play03:14

this also I'm going to talk this is just

play03:16

a start when it comes to car if we have

play03:19

any character type of data then we can

play03:22

use car primitive data type in order to

play03:25

store this particular value how we are

play03:28

going to store it what why there is a

play03:30

difference and why we have so many

play03:32

options I'm going to talk again I would

play03:33

like to repeat here Java supports these

play03:36

many primitive data types we do have

play03:39

nonprimitive also what if I want to

play03:42

store collection of data in one place we

play03:45

have a concept of arrays we have a

play03:47

concept of collection which I'm going to

play03:49

talk what if I want to store collection

play03:51

of characters not just one character we

play03:54

have a string which is not a primitive

play03:56

data type in Java which is considered as

play03:59

object then what is object what is

play04:01

non-primitive there is no Clarity yet

play04:03

I'm going to give you the clarity so now

play04:06

in the last video what we have come to a

play04:08

conclusion is Java is a strongly type

play04:11

programming language hence whenever we

play04:13

store a data or work with some data we

play04:16

may have to specify what type of data we

play04:20

are working on hence learning the type

play04:22

of data or data type is very very

play04:25

crucial because of which Java supports

play04:28

these many Prem itive data type let's

play04:31

understand one by one to start with

play04:33

first I would like to discuss the

play04:35

integer data types whenever we have a

play04:38

whole number to be stored we can go with

play04:42

integer data types if we have any whole

play04:46

number without any decimal point fine so

play04:50

let me just talk on this so whenever we

play04:52

have a whole number to be stored without

play04:55

any fractional point or decimal point

play04:57

example this is a whole number we don't

play04:59

have any decim deal Point here any it

play05:00

could be any any number normal number

play05:03

without any decimal point all of them

play05:05

are considered as whole number so in

play05:08

your Java application if you want to

play05:10

store a data which is of this then you

play05:13

can go with either of these data type

play05:15

you can go with bite you can go with

play05:19

chot you can go with int also you can go

play05:24

with the long what is the difference

play05:26

among this why we have four option that

play05:29

is very crucial and we may have to

play05:32

understand now for an instance I have a

play05:35

data I have a value maybe age I want to

play05:38

store someone's age so I can do one

play05:41

thing I can just create a variable like

play05:43

this maybe I'll say here age is a

play05:44

variable name data which I want to store

play05:47

is 45 great so behind the scene on your

play05:51

memory age memory will be allocated

play05:54

inside which you are specifying you're

play05:55

storing 45 45 will be added now since

play05:59

I'm going to store this data in Java

play06:02

programming language I have to specify

play06:05

what type of data it is the data type

play06:08

now since it is a whole number I can use

play06:11

bite also short also int also long also

play06:14

let me use bite now if you use bite what

play06:18

happens if you go with short what

play06:20

happens we may have to understand

play06:22

whenever you use bite data type in order

play06:25

to store a whole number behind the scene

play06:29

on your program on your memory one byte

play06:31

of memory will be allocated Ram is

play06:34

consist of bytes 8 gabt 16 gab GB 16 GB

play06:40

it's all about RAM and it is I'm talking

play06:42

about the memory when your program is

play06:44

executing program gets executed on the

play06:47

Ram from the ram only instructions will

play06:49

be given to the processor processor will

play06:51

execute this this fundamentals you

play06:53

should be knowing it so if you use bite

play06:56

data type to store some data behind the

play06:59

scene on your memory one bite of memory

play07:02

will be occupied the first thing if you

play07:05

go with short behind the scene on your

play07:08

memory two bytes of memory would be

play07:10

occupied if you go with int data type in

play07:14

order to store any value maybe one whole

play07:16

number you're storing and the type of

play07:18

the data which you have specified is int

play07:21

usually very often we go with int data

play07:23

type in most of the programming language

play07:25

including Java why so I'm going to talk

play07:28

when it comes to to int four bytes of

play07:31

memory will be allocated behind the

play07:33

scene in the memory if you go with long

play07:35

eight bytes of memory that is a first

play07:37

difference between them so let me just

play07:40

give a memory

play07:41

representation so the first difference

play07:43

which you have understood is with

play07:45

respect to memory occupancy if you use

play07:47

bite one bite of memory would be

play07:49

occupied and short two bytes integer

play07:51

four bytes and long8 bytes that is the

play07:54

first difference second difference if

play07:57

you go with bite you can store specific

play08:00

range of number when it comes to whole

play08:02

number it could be of any range right if

play08:05

you go with bite you cannot store every

play08:07

number there is a specific range between

play08:10

one to that only you can store how do we

play08:13

calculate range when it comes to bite

play08:15

one byte of memory would be occupied

play08:17

right one byte of memory equal to 8 Bits

play08:22

if you know already well and good if you

play08:25

don't know now you understand 1 bite is

play08:27

equal to 8 Bits fine now the formula is

play08:31

- 2^ n -1 2 2^ N -1 minus one where n is

play08:40

n is number of bits number of bits now

play08:45

one bite means it is 8 Bits so the range

play08:48

is - 2 power 8 - 7 n is 8 8 - 1 is 7

play08:54

sorry 8 - 1 is 7 2 2 power 7 -1 that

play09:00

means -

play09:02

128 to

play09:05

127 if there is any number between this

play09:09

range which is a whole number then you

play09:11

can go with bite data type and if you go

play09:14

with this behind the scene only one bite

play09:17

of memory would be occupied and easily

play09:19

you can manage any number between this

play09:21

range for an instance on your screen you

play09:23

can see I'm trying to store 45 variable

play09:27

age 45 comes within the range of- 128 to

play09:31

127 there is no problem here however if

play09:35

you try to cross the range which is 127

play09:38

it is not acceptable that is a

play09:40

difference number two same goes to short

play09:43

two bytes means it is 16 bytes so that

play09:45

means 16 bits that is 2 power 16 - 1 is

play09:50

15 2 2 power 15 minus 1 whatever value

play09:56

you get here that is a range you can go

play09:58

with if you go with with the short data

play10:00

type when it comes to integer it is 2

play10:03

power 32 - 1 which is 31 to 2 power 31-

play10:10

1 whatever result you'll get here that

play10:12

will be the range you can store if you

play10:14

go with in when it comes to long it is 8

play10:18

bytes that is equal to 64 bits so 2

play10:22

power 8 bytes one bite is equal to 8

play10:26

Bits one bite is equal to to 8 Bit And 8

play10:32

bytes that means 8 into 8 then we will

play10:36

get the result as 64 8 bytes into 8 bit

play10:41

so of course it is 64 so you'll be

play10:43

getting the result here 2 power 64 - 1

play10:47

which is 63 2 2 power 63 - 1 it is 64

play10:53

because I'm already considering n minus

play10:55

1 here it is 2^ 16 16 - 1 because n

play10:59

minus one here so whatever range you get

play11:02

that is a range you can store if you go

play11:04

with long that is the second you know

play11:07

difference so basically if there is a

play11:10

whole number without any decimal point

play11:12

or fractional value and in my Java

play11:16

application in my Java code if I want to

play11:18

store the data then I have four options

play11:22

available with me and the difference is

play11:25

first is memory optimization there is a

play11:27

difference with respect to memory occupy

play11:29

if you go with bite only one bite of

play11:32

memory would be occupied if you go with

play11:34

long then eight bytes of memory would be

play11:36

occupied and the second difference major

play11:39

difference is with respect to the range

play11:41

now if you want to store maybe small

play11:43

value if you want to store someone's age

play11:46

then definitely bite is sufficient bite

play11:48

can store someone's age I don't think so

play11:50

someone can live more than 127 years of

play11:53

course within this bite only you can

play11:55

manage maybe if you want to store a more

play11:58

data which is within the range of 2

play12:01

power 15- 2^ 15 to 2^ 15 minus 1

play12:06

whatever value you get here which I will

play12:07

be listing out in a minute here that

play12:09

range you can use with respect to short

play12:12

that is the difference between them not

play12:14

just that if there is any number in your

play12:18

Java code which is a whole number and if

play12:21

you have not specified any data type

play12:24

then the default data type of any number

play12:27

in Java is a in what do you mean by that

play12:31

I'll be showing you right now by opening

play12:33

the ID as you can see here I have opened

play12:36

the Eclipse IDE and there is a basic one

play12:39

Java program here if you are aware of

play12:42

java fundamentals also you know what is

play12:44

there on your screen if you are new to

play12:46

the world of java you need not to worry

play12:48

just understand this is a starting point

play12:50

of your program from here is where the

play12:52

program execution begins not more than

play12:54

that fine so inside this let me just

play12:57

write the code here more on this the

play12:58

first

play12:59

know class of java I would like to talk

play13:01

going forward also in the Java playlist

play13:04

now please don't focus on that so when

play13:06

it comes to data we have 45 and let me

play13:10

give the variable name as maybe N1

play13:13

equals to 45 45 comes within the range

play13:17

of- 128 to 127 so of course you can

play13:21

store there is no problem now in place

play13:24

this is of course 100% valid there is no

play13:26

problem in it now in place of 45 maybe I

play13:30

will try to use 130 you can see we are

play13:33

getting one compile time error Eclipse

play13:36

IDE int IDE these IDE has the auto

play13:40

compiler the moment you write the code

play13:42

Java code gets compiled you need not to

play13:45

automatically compile explicitly I mean

play13:47

you need not to explicitly compile it

play13:49

usually whenever you write a Java

play13:51

program you may have to compile then you

play13:53

have to execute fine but for now it is

play13:56

not needed because of autoc compilers

play13:59

anyway we are getting one error here

play14:01

compile time error it is not within the

play14:03

range of bite if I just give here 13 no

play14:06

problem 13 is within the range of bite

play14:09

same goes with respect to the short also

play14:11

let me just give it as short data type

play14:14

and if I have to specify maybe N2 equals

play14:17

to any number if I give which is within

play14:20

the range of short there is no problem

play14:22

see here it is outside the range of shot

play14:24

if I give here two no problem see 23 if

play14:27

I cross 30

play14:29

approximately it is

play14:32

32,768 is the range if you calculate 2

play14:36

power no 2^ 15 to 2 power 15 minus 1 it

play14:41

will be around

play14:43

32,768 approximately you can also

play14:45

calculate so if it is within that range

play14:47

there is no problem you can store it but

play14:49

if I cross the 32,000 part maybe I'll

play14:51

write four it is out of the range you

play14:54

cannot store right so memory 2 bytes

play14:57

here one by of memory and and behind the

play14:59

scenes specific range of data you can

play15:01

use when it comes to short two bytes of

play15:03

memory would be occupied and more than

play15:06

that a specific range only you can store

play15:08

you cannot surpass that range when it

play15:10

comes to integer if I go with integer

play15:13

type maybe number three equals to any

play15:16

range see here there is no problem the

play15:18

range is higher 2 power- 63 to um 2

play15:22

power - 31 to 2^ 31 -1 fine so that is a

play15:28

range the will be higher when it comes

play15:30

to int but behind the scene since I have

play15:32

used int data type on my memory four

play15:35

bytes of you know data four bytes of

play15:39

memory is occupied if I surpass here you

play15:41

can see here it is out of the range

play15:43

again you'll be getting the compet error

play15:45

so everything has a limit within that

play15:47

range only you can store the data when

play15:50

it comes to long you can of course store

play15:53

higher range data N4 equals to see here

play15:58

I'm storing higher range of data but

play16:00

still I'm getting error here whenever

play16:02

you want to store integer value higher

play16:05

than the range of in data type inside

play16:08

the long must and should you may have to

play16:11

add one suffix called as L here must and

play16:14

should you may have to add whether it is

play16:15

a capital l small L doesn't matter you

play16:18

may have to add here the moment I have

play16:20

added the error is gone fine so if you

play16:24

store a whole number with a range higher

play16:27

than integer L range whatever range we

play16:30

get in the int data type if I surpass

play16:32

that range and if I want to store that

play16:35

data inside the long must and should as

play16:38

per the rule of java you may have to

play16:39

specify one suffix suffix means after

play16:42

value after literal this can be called

play16:45

as literal value variable name type of

play16:49

the data type of data variable name and

play16:52

the actual value after actual value I'm

play16:55

going to add at the end suffix and

play16:57

behind the scene how many how many bytes

play16:59

of memories allocated here eight bytes

play17:01

of memory and every data will have a

play17:06

specific default data type for an

play17:08

instance I'll go with

play17:10

445 and 445 maybe I would like to

play17:14

specify some variable maybe I'll say it

play17:16

is some number variable name I have

play17:18

given it as num now I have not specified

play17:21

type of data

play17:23

455 of course you cannot store within

play17:25

the bite because bite maximum value is

play17:28

127 positive maximum value but it is

play17:31

within the range of short in the int

play17:34

also long also you can manage it now if

play17:37

I have to go here and if I say create

play17:39

the field you can see here what is the

play17:42

default data type my eclipse is

play17:43

generated in I have not coded it I have

play17:46

asked my Eclipse to generate which shows

play17:49

the default data type of any whole

play17:52

number in Java is int if you don't

play17:55

specify any data type the default data

play17:57

type is int which also shows that

play18:00

whenever we work with any of the whole

play18:02

number it is required it is recommended

play18:04

to go with int data type unless the

play18:07

value is of higher range because it is

play18:11

the default data type not just that if

play18:13

you perform any of the operation on the

play18:16

whole number the resultant is always

play18:19

integer what do we mean by that for an

play18:21

instance I will say here bite type of

play18:24

maybe number one equals to 45 great bite

play18:28

type of of maybe I'll say number two

play18:30

equals to 4 so 45 maybe I'll say here

play18:34

result equals to number one plus number

play18:37

two so inside number one we have 45 plus

play18:42

inside number two we have four 45 + 4 45

play18:47

+ 4 is 49 49 is within the range of bite

play18:53

however you cannot use that whenever you

play18:55

perform any of the operations on the

play18:59

whole number type of data in Java the

play19:02

resultant is always integer if I try to

play19:05

store in bite please notice here I'm

play19:07

getting one compile time error how can

play19:09

you store in bite you might say that 45

play19:12

+ 4 is 49 49 is within the range of bite

play19:17

but it is not recommended fine so the

play19:20

resultant is always integer so the point

play19:24

here is the default data type of whole

play19:27

number in Java is int Point number one

play19:30

point number two is whenever we perform

play19:32

any of the operation on the whole number

play19:35

the resultant is always integer this is

play19:38

what you have to understood now I hope

play19:40

the fundamentals related to the integer

play19:43

data types that means whenever we have a

play19:45

whole number if you want to store that

play19:48

or if you want to work with that what

play19:50

type of data we can use is clear to

play19:52

everyone fine now let's talk on the real

play19:56

data type real number data type we have

play19:58

float and double when it comes to real

play20:01

number if you have a real number which

play20:03

you want to store in your Java you can

play20:05

either go with float data type or you

play20:07

can go with the double data type real

play20:09

number referring to such a number which

play20:12

has the decimal point which has the

play20:13

fractional Point example

play20:16

45.4 maybe 4

play20:18

44.5 any number it could be where a

play20:21

decimal point is involved that number

play20:23

called as real number if you want to

play20:25

manage maybe someone's percentage how

play20:27

many percent you got in your metric in

play20:30

your college in your graduation you

play20:31

might say I have got

play20:33

70.5 such data if you want to manage in

play20:36

your Java application I have to create

play20:38

variable maybe I'll go and create the

play20:41

variable as average that's the variable

play20:43

name I have decided and I want to store

play20:46

the variable maybe

play20:48

72.4 great now this is the data which is

play20:52

going to get stored inside this variable

play20:55

since I'm using Java programming

play20:57

language I have to specify what type of

play21:00

data I'm going to store here the type of

play21:03

the data I can specify here is either

play21:05

float or I can go with double because

play21:08

these two data types if you use you can

play21:11

store any of the real number however if

play21:15

you go with float must and should after

play21:18

the literal after the value you may have

play21:20

to add f as the suffix if you don't add

play21:24

then it will not consider fine so maybe

play21:27

if I have to store a real number I'll

play21:30

say

play21:31

44.5 if I want to store without any

play21:34

suffix then I can go with double so the

play21:37

first difference is if you go with float

play21:40

then must and should you may have to use

play21:42

F and if you go with double you need not

play21:45

to use any suffix or prefix second point

play21:48

if you go with the float data type in

play21:50

order to store the real number four

play21:53

bytes of memory would be occupied when

play21:56

it comes to double 8 by bites of memory

play21:59

would be occupied if I use double data

play22:02

type in order to manage work or store

play22:05

the data which is of the real number

play22:09

then 8 bytes of my memory would be

play22:12

occupied apart from that the default

play22:15

data type of any real number in Java is

play22:18

double that means if you don't specify

play22:20

any data type and if you store then Java

play22:23

compiler will suggest you or will

play22:25

consider that type of the data is of

play22:28

double type let me show you the same in

play22:30

the Eclipse IDE by opening it as you can

play22:33

see here let me just write here I'll say

play22:35

float type of maybe average equals to

play22:39

45.5 if I don't specify C I'm getting

play22:41

one error here the moment I write one

play22:43

suffix F then there is no problem fine

play22:47

now if I go with double double type of

play22:50

maybe some data I'll say average 2 I

play22:53

will consider 44

play22:55

4.5 see I have not added any prefix or

play22:59

suffix still it is able to manage it

play23:02

however behind the scene for this four

play23:05

bytes of memory is occupied behind the

play23:08

scene for this eight bytes of memory is

play23:12

occupied that you need to understand

play23:15

here fine now apart from that if I don't

play23:18

specify maybe I'll say some random

play23:19

number this is a real number right and I

play23:22

want to store I will give here maybe

play23:24

I'll say here a real number that is a

play23:26

type a variable name I have given it is

play23:28

just a variable name type of data I have

play23:31

not specified and since we are using

play23:33

Java in this so in Java C C++ a strongly

play23:37

type programming languages it is

play23:39

compulsory for us to specify the type of

play23:41

the data I'm going to just do one thing

play23:44

I'll say here create a local variable

play23:46

see the default data type if you don't

play23:49

specify it is double so if there is a

play23:52

real number we can go with float and

play23:54

double if you go with float four bytes

play23:56

of memory if you go with double 8 by of

play23:58

memory and the default data type if you

play24:01

don't specify explicitly for any of the

play24:03

real number is always double that's

play24:06

about the real number now let's talk on

play24:09

the Boolean data type using Boolean data

play24:12

type you can store only two values one

play24:15

is true or false example if I have true

play24:19

to be stored then I can go with the

play24:23

maybe any variable name I'll say maybe a

play24:25

it is of Boolean type or I can go with

play24:29

false apart from these two things you

play24:32

cannot use anything you cannot use

play24:35

anything here either true or false

play24:38

nothing apart from that no number

play24:40

nothing within the double code single

play24:42

code nothing only true that two in the

play24:44

lower case only false apart from this

play24:47

nothing can be used but if I go with

play24:50

Boolean how many bytes of memory would

play24:52

be occupied there is no fixed bytes here

play24:54

it is jvm specific there is nothing

play24:56

called as fixed number of bytes which

play24:58

will be occupied here there is no

play25:00

documentation also for that available so

play25:02

if I go with Boolean I can use true or

play25:04

false what will be situation which you

play25:06

will understand going forward in the you

play25:08

know operators class also it is also

play25:10

very crucial data type whenever you want

play25:12

to check if something is happening or

play25:14

not we may have to use it it is very

play25:15

crucial data type which we will

play25:17

understand for now Boolean Miss true I

play25:20

can store false I can store that is more

play25:22

than sufficient for you to understand

play25:25

apart from that we do have one more

play25:27

which is car whenever we have a

play25:29

characters to be stored any character it

play25:31

could be it could be alphabets it could

play25:34

be numbers it could be any special

play25:37

characters whatever special characters

play25:39

anything anything within the single code

play25:42

is considered as a character there is a

play25:45

character if I write even number but if

play25:48

that number is there within the single

play25:50

code it is a character it is one

play25:52

character and if you want to store that

play25:54

in your Java application and if you want

play25:56

to use any of the primitive data types

play25:58

then you have the option of car variable

play26:00

name maybe I'll give it as uh some

play26:03

character whatever the variable name

play26:04

doesn't matter so let me use it as a and

play26:07

the type of the data is car so this is

play26:10

value this is the variable name and this

play26:13

is the type of data behind the scene two

play26:16

bytes of memory would be occupied here

play26:19

two bytes of memory and here also it is

play26:22

not considered as a number now it is a

play26:25

character maybe I'll store in a variable

play26:27

called as B with care can we write a

play26:30

multiple characters within the single

play26:32

code no it is invalid it should be a

play26:35

single character it must be in single

play26:38

code writing the single code is also

play26:40

mandatory and how many bytes of memory

play26:43

behind the scene two bytes of memory

play26:45

this fundamentals for now is sufficient

play26:48

again in my Java playlist I'm going to

play26:50

talk detailed on the data type what is

play26:53

the difference between asky format uni

play26:56

code format what is the need of

play26:58

character data type what is the work of

play27:00

the data types I'm going to specify in

play27:02

detail for now in our series to continue

play27:04

in the DSA part this fundamentals on the

play27:08

data type is more than sufficient if you

play27:10

have whole number go with int if you

play27:13

have a real number go with double or

play27:14

float if we have something to store in

play27:17

know true or false go with Boolean if we

play27:19

have any

play27:21

characters then we can go with care what

play27:24

if we have a collection of data which is

play27:25

of string what if we have large volume

play27:28

of data in one place we may have to

play27:30

specify which is array about this

play27:33

separately we are going to have the

play27:34

discussion they are nonprimitive they

play27:37

are considered as objects in Java how to

play27:40

store collection of data how to store

play27:42

collection of characters within double

play27:44

quotes arrays strings collections link

play27:47

list all those things we are going to

play27:49

talk going forward in this series this

play27:52

is about fundamentals of data types why

play27:55

we have to learn data types what is the

play27:57

dat type how we can use it what is the

play28:00

difference among them fundamentally I

play28:02

hope you have got the clarity it is more

play28:05

than sufficient to go ahead in this

play28:07

series again there is a lot of scope to

play28:10

talk in data types also in my paid

play28:12

batches if you already part of it if

play28:14

your friend is part of it you can ask I

play28:16

will have a detail uh hours of

play28:18

discussion on data type not needed right

play28:20

now maybe in my Java series if you want

play28:23

more than this to be learned that will

play28:24

be discussed I felt this is more than

play28:27

sufficient to go ahead ahead in this

play28:29

series so thank you so much for being

play28:31

with me in this video let's learn more

play28:33

in the upcoming videos please do

play28:35

subscribe this Channel please do like

play28:37

the video and also share among your

play28:40

friends thank you so much for being with

play28:41

me in this video

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Java BasicsData TypesPrimitive TypesStrongly TypedInteger TypesReal NumbersBoolean LogicCharacter TypeMemory AllocationProgramming TutorialJava Fundamentals