Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
24 views
14 pages
#3
Uploaded by
Star Geezer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save #3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
24 views
14 pages
#3
Uploaded by
Star Geezer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save #3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save #3 For Later
You are on page 1
/ 14
Search
Fullscreen
Java Data Types Read Discuss(20+) Courses Practice ‘Video Java is statically typed and also a strongly typed language because, in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types. What are Data Types in Java? Data types in Java are of different sizes and values that can be stored in the variable that Java Arrays JavaStrings JavaOOPs JavaCollection Java Tutorial Java Multithreading Java Except categories in which data types are segregated 1. Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double 2. Non-Primitive Data Type or Object Data type: such as String, Array, ete. Primitive Data Types in Java Primitive data are only single values and have no special capabilities. There are 8 primitive data types. They are depicted below in tabular format below as follows:Type |Description Default Size Example Range of values Literals boolean true orfalse false 1bit | true, false true, false byte | twos- 6 complement | 0 {none} -128 to 127 bits integer char characters representation ¢ Unicod ag | 2 \w0042 ASCII values ues \uoo00 ‘\102', WV, "in character bits 8 Oto 255 short | twos- 16 complement | 0 bis (none) -32,768 to 32,767 i integer int -2,147,483,648 twos- 32 complement | 0 -2,-1,0,1,2 to bits intger 2,147,483,647 long -9,223,372,036,854,775,8C twos- or complement | 0 oe -2L,-1L,0L,1L,2L to its integer 9,223,372,036,854,775,80Type |Description Default Size | Example Range of values Literals float | Ieee 754 32 b23el00F, floating 0.0 sie -1.23e-100F, .3f upto 7 decimal digits its point .3.14F double | ieee 754 4 | 12345603004, a floating 0.0 bite 7123456e-3004 upto 16 decimal digits point mS | ted Let us discuss and implement each one of the following data types that are as follows: 1. Boolean Data Type Boolean data type represents only one bit of information either true or false which is intended to represent the two truth values of logic and Boolean algebra, but the size of the boolean data type is virtual machine-dependent. Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code. Syntax: boolean booleanVar; Size: Virtual machine dependent 2. Byte Data Type The byte data type is an 8-bit signed two's complement integer. The byte data type is useful for saving memory in large arrays. Syntax: byte bytevar; Size: 1 byte (8 bits) 3. Short Data TypeThe short data type is a 16-1 it signed two's complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters. Syntax: short shortVar; Size: 2 bytes (16 bits) 4. Integer Data Type Itis a 32-bit signed two's complement integer. Syntax: int intvar; Size: 4 bytes ( 32 bits ) Remember: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has a value in the range [0, 232-1]. Use the Integer class to use the int data type as an unsigned integer. 5. Long Data Type The range of a long is quite large. The long data type is a 64-bit two's complement integer and is useful for those occasions where an int type is not large enough to hold the desired value. The size of the Long Datatype is 8 bytes (64 bits). Syntax: long longvar3 Remember: In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 24.1. The Long class also contains methods like comparing Unsigned, divide Unsigned, etc to support arithmetic operations for unsigned long.6. Float Data Type The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits). ‘Syntax: float floatvar; 7. Double Data Type The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. The size of the double data type is 8 bytes or 64 bits, Syntax: double doubleVar; Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use BigDecimal class instead. Itis recommended to go through founding off errors in java. 8. Char Data Type The char data type is a single 16-bit Unicode character with the size of 2 bytes (16 bits) Syntax: char charVar; Why is the Size of char 2 bytes in Java? So, other languages like C/C++ use only ASCII characters, and to represent all ASCII characters 8 bits is enough. But java uses the Unicode system not the ASCII code systemand to represent the Unicode system 8 bits is not enough to represent all characters so java uses 2 bytes for characters. Unicode defines a fully international character set that, can represent most of the world's written languages. It is a unification of dozens of character sets, such as Latin, Greek, Cyrillic, Katakana, Arabic, and many more. Example: Java // Java Program to Demonstrate Char Primitive Data Type // Class class GFG { // Main driver method public static void main(String args[]) t // Creating and initializing custom character char a = 'G'; // Integer data type is generally // used for numeric values int i = 89; // use byte and short // if memory is a constraint byte b = 4; // this will give error as number is // larger than byte range // byte b1 = 7888888955; short s = 56; // this will give error as number is // larger than short range // short s1 = 87878787878; // by default fraction value 71 is double in java double d = 4.355453532; // for float use 'f' as suffix as standard float f = 4.7333434f; // need to hold big range of numbers then we need // this data type Jong 1 = 12222; system. out. print1n( system.out.printIn(*i system. out. print1n( system. out.print1n(system.out.printIn("float: " + f); system. out. printin, 4 d)5 system, out. print1n, : "413 Output char: G integer: 89 byte: 4 short: 56 float: 4.7333436 double: 4.355453532 long: 12121 Non-Primitive Data Type or Reference Data Types The Reference Data Types will contain a memory address of variable values because the reference types won't store the variable value directly in memory. They are strings, objects, arrays, etc. 1. Strings Strings are defined as an array of characters. The difference between a character array and a string in Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char-type entities. Unlike C/C++, Java strings are not terminated with a nulll character. Syntax: Declaring a string
= “
”; Example: // Declare String without using new operator String s = "GeeksforGeeks"; // Declare String using new operator String s1 = new String("GeeksforGeek 2. ClassA glass is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: 1. Modifiers: A class can be public or has default access. Refer to access specifiers for classes or interfaces in Java 2. Class name: The name should begin with an initial letter (capitalized by convention) 3. Superclass(if any): The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. 4. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. 5. Body: The class body is surrounded by braces, {} 3. Object An Object is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of 1. State: It is represented by the attributes of an object. It also reflects the properties of an object. 2. Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects. 3. Identity: It gives a unique name to an object and enables one object to interact with other objects. 4. Interface Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body) * Interfaces specify what a class must do and not how. Itis the blueprint of the class. * An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement. * Ifa class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract. + A lava library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection. 5. ArrayAn Array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++. The following are some important points about Java arrays. * In Java, all arrays are dynamically allocated. (discussed below) * Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using size. * AJava array variable can also be declared like other variables with [J after the data type. * The variables in the array are ordered and each has an index beginning with 0. * Java array can also be used as a static field, a local variable, or a method parameter. * The size of an array must be specified by an int value and not long or short. * The direct superclass of an array type is Object. * Every array type implements the interfaces Cloneable and java.io.Serializable. Frequently Asked Questions: 1. What are data types in Java? Data types are of different sizes and values that can be stored in the variable that is made as per convenience and circumstances to cover up all test cases. 2. What are the 8 data types that use in Java? There are 8 main primitive data types in java as mentioned below: * boolean © byte © char * short * int * long © float * double 3. Which is a primitive type? Primitive data types are the types in java that can store a single value and do not provide any special capability. 4, Why char uses 2 bytes in java and what is \u0000?Char uses 2 bytes in java because it uses the Unicode system rather than the ASCII system. “\u000" is the lowest range of the Unicode system Check Out: Quiz on Data Type in Java This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write geeksforgeeks.org or mail your article to
[email protected]
. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. Last Updated : 27 Mar, 2023 © Similar Reads 1. Java Program For Arithmetic Operations Between BigDecimal and Primitive Data Types How to Get Size, Minimum, and Maximum Value of Data Types in Java? Default Values Assigned to Primitive Data Types in Java Primitive data type vs. Object data type in Java with Examples 5. Converting Integer Data Type to Byte Data Type Using Typecasting in Java 6. Difference Between java.sqlTime, java.sql Timestamp and java.sql.Date in Java 7. Types of Exception in Java with Examples 8. Types of References in Java 9, Covariant Return Types in Java 10. Bounded Types with Generics in Java Related Tutorials 1. Spring MVC Tutorial 2. Spring TutorialSpring Boot Tutorial Java 8 Tutorial Java l0 Tutorial Previous Article Contributed By : eo GeeksforGeeks Vote for difficulty Current difficulty : Easy easy |[ wormat |[ vec ][ vod ) [emer Improved By: ‘ShreyasWaghmare, RishabhPrabhu, pranitamahandule, sofia, nishkarshgandhi, tajammulbasheer, nandinigujral, ankurSoz5 Article Tags: java-basics, Java Practice Tags: Java Improve Article Report Issue solankimayank, Next A-143, 9th Floor, Sovereign Corporate Tower, Sector-136, Noida, Uttar Pradesh - 201305
[email protected]
Company Explore About Us Job Fair For Students Careers POTD: Revampedin Media Contact Us Terms and Conditions Privacy Policy Copyright Policy Third-Party Copyright Notices Advertise with us Languages Python Java cH GoLang sql R Language Android Tutorial Algorithms Sorting Searching Greedy Dynamic Programming Pattern Searching Backtracking Computer Science GATE CS Notes Operating Systems Computer Network Database Management System Software Engineering Digital Logic Design Engineering Maths Data Science & ML Data Science With Python Python Backend LIVE Android App Development DevOps LIVE DSA in JavaScript Data Structures Array String Linked List Stack Queue Tree Graph Web Development HTML css JavaScript Bootstrap Reacts Angulards Nodes Python Python Programming Examples Django Tutorial Python Projects Python Tkinter Opency Python Tutorial Python interview Question DevOps GitData Science For Beginner Machine Learning Tutorial Maths For Machine Learning Pandas Tutorial NumPy Tutorial NLP Tutorial Deep Learning Tutorial Competitive Programming Top DSA for CP Top 50 Tree Problems Top 50 Graph Problems ‘Top 50 Array Problems ‘Top 50 String Problems Top 50 DP Problems ‘Top 15 Websites for CP Interview Corner Company Preparation Preparation for SDE Company Interview Comer Experienced Interview Internship Interview Competitive Programming Aptitude Commerce Accountancy Business Studies Microeconomics Macroeconomics Statistics for Economics Indian Economic Development ‘SSC/ BANKING SSC CGL Syllabus AWS Docker Kubernetes paure cep ‘System Design What is System Design Monolithic and Distributed SD Scalability in SD Databases in SD High Level Design or HLD Low Level Design or LLD ‘Top SD interview Questions GfG School CBSE Notes for Class 8 CBSE Notes for Class 9 CBSE Notes for Class 10 CBSE Notes for Class 11 CBSE Notes for Class 12 English Grammar upsc Polity Notes Geography Notes History Notes Science and Technology Notes Economics Notes Important Topics in Ethics UPSC Previous Year Papers Write & Earn Write an Article‘SBIPO Syllabus Improve an Article SBI Clerk Syllabus Pick Topics to Write 1BPS PO Syllabus Write interview Experience IBPS Clerk Syllabus Internships Aptitude Questions Video internship SSC CGL Practice Papers @geeksforgeeks , Some rights reserved
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6458)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (643)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (650)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (1005)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (464)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (141)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (5181)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (298)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (582)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1175)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4.5/5 (1856)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (836)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4.5/5 (1139)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2289)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (144)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (2016)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (244)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (629)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
4/5 (278)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (1022)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (2885)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1090)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (78)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (233)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2546)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2814)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4135)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (943)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (903)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (280)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4.5/5 (4103)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M.L. Stedman
4.5/5 (815)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (919)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1267)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2133)
Little Women
From Everand
Little Women
Louisa May Alcott
4.5/5 (2369)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4372)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (2033)