Explore the English language on a new scale using AI-powered English language navigator.
Types in JavaBefore one can use a varialbe in Java, it must be declared. Every variable has a type, which indicates kind of data variable stores and possible operations on that data. As it was mentioned in previous tutorials, Java is an object-oriented programming language. However, developers have decided to include support of primitive data types in Java. Primitive typesVariables of primitive types can store integer and fractional numbers, single characters and true/false logical values. You may think of those types as of the atomic ones, because they are bricks to constuct more complicated ones. Primitive types are predefined by Java and their names are reserved keywords. There are eight primitive types in Java programming language:
Note, that all primitive types in Java are signed. Every primitive type has a corresponding nonprimitive wrapper. Reference typesOther types in Java (reference types), including String one, are related to specific classes, interfaces or arrays. By default, all variables of reference types are set to null. ExampleIt's useful to play with variables and types a little. We'd like to show an example below, demonstrating a simple code working with primitive types. Types.javapublic class Types { public static void main(String[] args) { // byte int byteVar = 100; System.out.println("byte variable's value: " + byteVar); // short int shortVar = 10000; System.out.println("short variable's value: " + shortVar); // int int intVar = 1000000000; System.out.println("int variable's value: " + intVar); // long long longVar = 1000000000000L; System.out.println("long variable's value: " + longVar); // float float floatVar = 1.1f; System.out.println("float variable's value: " + floatVar); // double double doubleVar = 1.12345d; System.out.println("double variable's value: " + doubleVar); // boolean boolean booleanVar = true; System.out.println("boolean variable's value: " + booleanVar); // char char charVar = 'Z'; System.out.println("char variable's value: " + charVar); // bonus: String String stringVar = "Hi! I am a string."; System.out.println("String variable's value: " + stringVar); } } Outputbyte variable's value: 100
Partners Ads
|