banner



How To Check The Data Type In Java

Data types are different sizes and values that can exist stored in the variable that is made as per convenience and circumstances to cover up all examination cases. Also, let the states cover up other important ailments that there are majorly 2 types of languages that are every bit follows:

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 programme must be described with ane of the data types.

Primitive data are but unmarried values and accept no special capabilities. At that place are 8 primitive data types. They are depicted below in tabular format below as follows:

Java Primitive Data Type

Let us talk over and implement each i of the following data types that are every bit follows:

Type 1: boolean

Boolean data type represents simply one bit of information either true or faux, but the size of the boolean data type is virtual auto-dependent. Values of blazon boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer tin can easily write conversion lawmaking.

Syntax:

boolean booleanVar;

Size: Virtual machine dependent

Values: Boolean such as true, false

Default Value: faux

Case:

Java

class GFG {

public static void principal(String args[])

{

boolean b = true ;

if (b == true )

System.out.println( "Howdy Geek" );

}

}

Type 2: byte

The byte information type is an eight-bit signed two's complement integer. The byte data type is useful for saving memory in big arrays.

Syntax:

byte byteVar;

Size: 1 byte (8 bits)

Values: -128 to 127

Default Value: 0

Example:

Java

course GFG {

public static void main(String args[]) {

byte a = 126 ;

System.out.println(a);

a++;

System.out.println(a);

a++;

Organization.out.println(a);

a++;

System.out.println(a);

}

}

Type iii: brusque

The brusk information blazon is a 16-fleck signed two's complement integer. Like to byte, apply a brusk to save retentivity in large arrays, in situations where the memory savings actually matters.

Syntax:

curt shortVar;

Size: 2 byte (16 bits)

Values: -32, 768 to 32, 767 (inclusive)

Default Value: 0

Type 4: int

Information technology is a 32-bit signed two's complement integer.

Syntax:

int intVar;

Size: 4 byte ( 32 $.25 )

Values: -ii, 147, 483, 648 to ii, 147, 483, 647 (inclusive)

Note: The default value is '0'

Call back: In Java SE eight and later, nosotros can use the int data blazon to represent an unsigned 32-bit integer, which has a value in the range [0, 232-1]. Utilize the Integer course to use the int data type as an unsigned integer.

Type 5: long

The long data type is a 64-bit ii'southward complement integer.

Syntax:

long longVar;

Size: 8 byte (64 bits)

Values: {-9, 223, 372, 036, 854, 775, 808} to {9, 223, 372, 036, 854, 775, 807} (inclusive)

Note: The default value is '0'.

Remember: In Java SE 8 and later, you lot tin use the long data type to represent an unsigned 64-fleck long, which has a minimum value of 0 and a maximum value of 264-i. The Long class besides contains methods like comparing Unsigned, divide Unsigned, etc to support arithmetics operations for unsigned long.

Type 6: float

The float data type is a single-precision 32-scrap IEEE 754 floating-point. Use a bladder (instead of double) if you need to save memory in large arrays of floating-point numbers.

Syntax:

float floatVar;

Size: 4 byte (32 bits)

Values: upto 7 decimal digits

Note: The default value is '0.0'.

Instance:

Java

import java.io.*;

course GFG {

public static void primary(String[] args)

{

float value2 = ix .87f;

System.out.println(value2);

}

}

If we uncomment lines no xiv,15,16 then the output would have been totally unlike as nosotros would have faced an error.

Type 7: double

The double information type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice.

Syntax:

double doubleVar;

Size: viii bytes or 64 bits

Values: Upto 16 decimal digits

Note:

  • The default value is taken every bit '0.0'.
  • Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern so, it is recommended not to utilize these data types and use BigDecimal grade instead.

It is recommended to get through rounding off errors in java.

Type eight: char

The char data type is a single 16-flake Unicode grapheme.

Syntax:

char charVar;

Size: 2 byte (16 bits)

Values: '\u0000' (0) to '\uffff' (65535)

Note: The default value is '\u0000'

You must be wondering why is the size of char 2 bytes in Java?

So, in other languages like C/C++ uses only ASCII characters, and to represent all ASCII characters 8-$.25 is enough. Just java uses the Unicode system not the ASCII code system and to represent the Unicode system 8 $.25 is not enough to represent all characters so java uses 2 bytes for characters. Unicode defines a fully international character prepare that can represent nearly of the globe's written languages. It is a unification of dozens of character sets, such as Latin, Greeks, Cyrillic, Katakana, Arabic, and many more.

Instance:

Java

class GFG {

public static void main(Cord args[])

{

char a = 'G' ;

int i = 89 ;

byte b = four ;

short s = 56 ;

double d = four.355453532 ;

float f = 4 .7333434f;

System.out.println( "char: " + a);

System.out.println( "integer: " + i);

System.out.println( "byte: " + b);

Organisation.out.println( "curt: " + s);

System.out.println( "float: " + f);

Arrangement.out.println( "double: " + d);

}

}

Output

char: G integer: 89 byte: 4 brusk: 56 float: 4.7333436 double: iv.355453532

Non-Archaic Information Blazon or Reference Data Types

The Reference Data Types will contain a retentivity address of variable values considering the reference types won't store the variable value direct in retentivity. They are strings, objects, arrays, etc.

Strings are defined as an assortment 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 zilch character.

Syntax: Declaring a string

<String_Type> <string_variable> = "<sequence_of_string>";

Example:

// Declare String without using new operator  String s = "GeeksforGeeks";   // Declare String using new operator  Cord s1 = new String("GeeksforGeeks");        

A: Class

A class is a user-defined blueprint or prototype from which objects are created.  Information technology represents the gear up of properties or methods that are common to all objects of i type. In full general, class declarations can include these components, in guild:

  1. Modifiers: A class can be public or has default access. Refer to access specifiers for classes or interfaces in Java
  2. Course proper noun: The name should begin with an initial letter of the alphabet (capitalized past convention).
  3. Superclass(if any): The proper noun of the grade's parent (superclass), if any, preceded past the keyword extends. A form tin can only extend (subclass) 1 parent.
  4. Interfaces(if any): A comma-separated listing of interfaces implemented past the class, if any, preceded by the keyword implements. A class can implement more than ane interface.
  5. Body: The class body is surrounded by braces, { }.

B: Object

It 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: Information technology is represented past the attributes of an object. Information technology besides reflects the properties of an object.
  2. Behavior: Information technology is represented past the methods of an object. It likewise 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.

C: Interface

Similar a class, an interface tin can have methods and variables, but the methods declared in an interface are past default abstract (only method signature, nobody).

  • Interfaces specify what a class must do and not how. It is the blueprint of the course.
  • An Interface is about capabilities similar a Histrion may be an interface and any form implementing Histrion must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
  • If a course implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstruse.
  • A Java library example is Comparator Interface. If a grade implements this interface, then it can be used to sort a drove.

D: Array

An 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 most Coffee arrays.

  • In Coffee, all arrays are dynamically allocated. (discussed below)
  • Since arrays are objects in Java, we tin can discover their length using member length. This is different from C/C++ where we find length using size.
  • A Java array variable can likewise be declared similar other variables with [] later the data type.
  • The variables in the assortment are ordered and each has an index beginning from 0.
  • Java array can also be used every bit a static field, a local variable, or a method parameter.
  • The size of an assortment must be specified by an int value and non long or short.
  • The direct superclass of an assortment type is Object.
  • Every array type implements the interfaces Cloneable and java.io.Serializable.

Check Out: Quiz on Data Blazon in Java

This article is contributed by Shubham Agrawal. If you similar GeeksforGeeks and would like to contribute, you can likewise write an article using write.geeksforgeeks.org or post your article to review-team@geeksforgeeks.org. See your commodity appearing on the GeeksforGeeks principal page and assist other Geeks. Please write comments if you observe annihilation incorrect, or yous want to share more than data most the topic discussed above.


How To Check The Data Type In Java,

Source: https://www.geeksforgeeks.org/data-types-in-java/

Posted by: morrishisems.blogspot.com

0 Response to "How To Check The Data Type In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel