Comprehensive parsing of Java-supported data types and Java constants and variable types _java

Source: Internet
Author: User
Tags reserved value of pi visibility

Basic data types
A variable is a memory location that is reserved for storing values. This means that when you create a variable, it takes up a certain amount of space in memory.

Based on the variable data type, the operating system allocates memory and determines what will be stored in the reserved memory. So, by assigning different data types to variables, you can store integers, decimals, or letters in these variables.

There are two kinds of valid data types in Java:

    1. Raw data type
    2. Reference data type

Raw data type

Java supports 8 of original data types. The original data type is predefined by the language and is named after the keyword. Let's take a closer look at these 8 types of data.

byte type (in byte)

The byte type is a 8-bit positive binary integer
The minimum value is-128 ( -2^7)
The maximum value is 127 (2^7-1)
The default value is 0
byte-type data types are primarily intended to conserve space in large arrays, primarily as substitution integers, which are 4 times times smaller than integers.
For example:

Byte a = $, byte B =-50

Short integer (shorter)

A short integer is a 16-bit positive binary integer
The minimum value is-32768 ( -2^15)
The maximum value is 32767 (2^15-1)
Data of short integer types can also be used to save space, like byte type. Short integers twice times smaller than integers
The default value is 0
For example:

Short S = 10000, short r =-20000

Integer type (int)

An integral type is a 32-bit positive binary integer
The minimum value is-2,147,483,648 ( -2^31)
The maximum value is 2,147,483,647 (2^31-1)
The integer type is typically applied to integer values unless you are worried about insufficient memory.
The default value is 0
For example:

int a = 100000, int b =-200000

Long integer

A long integer is a 64-bit positive binary integer
The minimum value is-9,223,372,036,854,775,808 ( -2^63)
The maximum value is 9,223,372,036,854,775,807 (2^63-1)
This type of data is generally applied when it needs to be larger than the integer range.
The default value is 0L
For example:

Long a = 100000L, int b = -200000l

Floating-point type (float)

Floating-point data is a single-precision floating-point data under the 32-bit IEEE 754 standard.
Floating-point data is intended primarily to conserve memory in large floating-point numeric arrays.
The default value is 0.0f.
Floating-point data cannot be used for precise data such as currency.
For example:

Float F1 = 234.5f

Doubles (double)

Double data is a double-precision floating-point data under the 64-bit IEEE 754 standard.
This data type is primarily the default value that is used to represent decimals, which is generally the default choice.
Double data cannot be used for precise data such as currency.
The default value is 0.0d
For example:

Double D1 = 123.4

Boolean (Boolean)

The Boolean data represents a bit of information.
It has only two possible values: True (True) and False (false)
This data type is used for simple tags under true and false conditions.
Default value is False (false)
For example:

Boolean one = True

Character type (char)

Character data is a simple character under the 16-bit Unicode standard.
The minimum value is: ' \u0000 ' (or 0).
The maximum value is: ' \uffff ' (or 65,535).
Character data can be used to store any letter.
For example: Char letter A (character type a) = ' a '
Reference Data type
reference data types are defined by the editor of the class. They are used to access objects. These variables are defined as specific types that cannot be changed. For example: Employee, Puppy and so on.
class objects and array variables are the reference data types.
The default value for any reference data type is null.
A reference data type can be used with objects of any declared type and compatible type.
For example:

Animal Animal = new Animal ("giraffe");

Java Constants
A constant is the source code that represents a fixed value. They are represented directly in the form of code without any estimate.
Constants can be assigned to any of the original variable types. For example:

byte a =;
char a = ' a '

Byte, Integer, long, and short integers can also be represented by decimal, hexadecimal, and octal counting systems.

When using these technical systems to represent the direct amount, prefix 0 is to mark Octal, and the prefix 0x is to mark hexadecimal. For example:

int decimal = m;
int octal = 0144;
int hexa = 0x64;

The rules for string constants in Java, like most other languages, are written in the middle of double quotes. Examples of string direct quantities are as follows:

"Hello World", "
two\nlines"
"\" is in Quotes\ ""

Character and string constants can contain arbitrary Unicode letters. For example:

char a = ' \u0001 ';
String a = "\u0001";

The Java language also supports some special escape sequences for character and string direct quantities. They are:

Escape character Meaning
\ n Line Wrap (0x0a)
\ r Carriage return (0x0d)
\f Page Change (0x0c)
\b BACKSPACE (0x08)
\s Space (0x20)
\ t tab
\" Double quotes
\' Single quotation mark
\ Back slash
\ddd octal character (DDD)
\uxxxx Hexadecimal UNICODE character (xxxx)

Variable Type
A variable can give us a named store that our program can manipulate. Each variable in Java has a specific type, which determines the size of the variable and its design takes up memory space; the values of these columns can be stored in that memory space; the actions that the variable can apply.

You must now declare the variables you want to use before you use them. The basic format for declaring variables is as follows:

data type variable [= value][, variable [= value] ...];
The data type here is a variable of Java, which is the name of a variable. To declare more than one specific variable type, you can separate it with commas.

The following are examples of valid variable declarations and assignments in Java:

int A, b, C; Declares three INTs, a, B, and C.
int a = ten, B = 10; Example of initialization
byte B =//Initializes a byte type variable B.
Double pi = 3.14159; Declares and assigns a value of PI.
char a = ' a '; The char variable a IIS initialized with value ' a '

There are three kinds of variables in the Java Communist Party:

    1. Local variables
    2. Instance variables
    3. class, Static variable

Local variables

    • Local variables are declared in a method, constructor, or block
    • The local variable is created when the method, constructor, or block is entered, and the variable is destroyed once it exits.
    • Accessible descriptor not available for local variables
    • Local variables are visible only in methods, constructors, or blocks that have been declared
    • Local variables implemented inside stack depth
    • The local variable has no default value, so the local variable must be declared and assigned to it before it is used for the first time.

Example

Here, age is a local variable. This is defined under the Pupage () method, and its scope is limited to this method.

public class test{public 
 void Pupage () {
  int age = 0;
  Age = age + 7;
  System.out.println ("Puppy Age are:" + age);
 }

 public static void Main (String args[]) {
  test test = new test ();
  Test.pupage ();
 }


The above code outputs the following results:

Puppy Age Is:7

Example
The following example uses the local variable age but does not initialize, so an error is displayed when editing.

public class test{public 
 void Pupage () {
  int age;
  Age = age + 7;
  System.out.println ("Puppy Age are:" + age);
 }

 public static void Main (String args[]) {
  test test = new test ();
  Test.pupage ();
 }


The following error occurs when editing:

Test.java:4:variable number might not have been initialized age
= age + 7;
^
1 Error

Instance variables

    • An instance variable is declared in a class, but it is outside a method, constructor, or block.
    • When an object in the heap is allocated a space, the position of each instance variable is created.
    • The instance variable is created when the object is created with the keyword "new", and it is destroyed when the object is destroyed.
    • The value of an instance variable must be referenced by more than one method, constructor, or block, or an important part of the state of the object that must appear in the class.
    • Instance variables can be declared at the class level before or after use.
    • Instance variables can be accessed with an accessible descriptor.
    • Instance variables are visible to all methods, constructors, or blocks in a class. Generally, it is recommended that these variables be private (access level). However, the visibility of subclasses can be given to those variables by an accessible descriptor.
    • The instance variable has a default value. The default value for the number is zero, the Boolean default is False, and the default value for the object reference is null. You can assign values within a declaration or constructor.
    • Instance variables can be accessed directly in the class by means of a name. However, the fully qualified name should be used in static methods and in different classes (instance variables can be accessed). Objectreference.variablename

Example

Import java.io.*;

public class employee{
 //This instance the variable is visible to any child class.
 public String name;

 Salary variable is visible into Employee class only.
 private double salary;

 The name variable is assigned in the constructor. 
 Public Employee (String empname) {
  name = EmpName;
 }

 The salary variable is assigned a value.
 public void Setsalary (double empsal) {
  salary = empsal;
 }

 This method prints the employee details.
 public void Printemp () {
  System.out.println ("Name:" + name);
  System.out.println ("Salary:" + salary);
 }

 public static void Main (String args[]) {
  Employee Empone = new Employee ("Ransika");
  Empone.setsalary (1000);
  Empone.printemp ();
 }


The above code outputs the following results:

Name:ransika
salary:1000.0

class, Static variable

    • A class variable is also called a static variable, which is declared in a class with the static keyword, but it is outside the method, constructor, or block.
    • There is only one class variable in each class, regardless of how many objects this class has.
    • In addition to being declared as constants, class variables are rarely applied. Constants are variables declared as public, private, final, and static. The initial value of an instance variable is not changed.
    • Static variables are stored in static memory, with little static variables rather than declaration ends or one with constant public or private.
    • Static variables begin and end as the program begins and ends.
    • Visibility and instance variables are similar. However, most static variables are declared public because they must be used by the users of the class.
    • The default value is similar to the instance variable. The default value for a number is zero, the Boolean default is False, and the object reference default is null. You can assign values within a declaration or constructor. In addition, you can assign a value in a special static initialization area.
    • Static variables can be accessed with the name of the class. Classname.variablename
    • When a static variable is declared as public static final, the variable (constant) name is capitalized. If the static variable is not public and final, its named method is the same as the instance variable and the local variable.

Example

Import java.io.*;

public class employee{
 //Salary variable is a private static variable
 private static double salary;

 DEPARTMENT is a constant public
 static final String DEPARTMENT = "Development";

 public static void Main (String args[]) {
  salary = 1000;
  System.out.println (department+ "average salary:" +salary);
 }


The above code outputs the following results:

Development Average salary:1000

Note: If a variable is accessed from outside the class, the constant must be accessed in employee.department.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.