Java Section No. 09 assignment and initialization

Source: Internet
Author: User

2016-06-28

1 Assigning values
Assignment: is assigning a value to a variable or changing the value of a variable.
int speed=30;
variable=expression;//variable-type variable name = Expression

 Packageday09; Public classTest { Public Static voidMain (string[] args) {//declaring Variables        intspeed,salary; floattime,basicsalary,prize,distance; Charalphabet; //assigning values to variablesAlphabet= ' a '; Basicsalary=3094.5f;//Base Salaryprize=2344.55f;//BonusesSpeed=30; time=3.4f; //Assigning a value to a variable through an expressionSalary= (int) (basicsalary+prize);//Total SalaryDistance=speed*time;//distance = speed * TimeSystem.out.println ("Letter Alphabet=" +alphabet); System.out.println ("Base salary basicsalary=" +basicsalary); System.out.println ("Bonus prize=" +prize); System.out.println ("Total wage salary=" +salary); System.out.println ("Distance distance=" +distance); }}

2 initialization
Local Variables : must be explicitly initialized.
instance variables and class variables : The compiler can initialize them automatically.
Boolean false
Char ' \000 '
BYTE0 (byte) 0000 0000
Short0 (short) 0000 0000 0000 0000
Int0
long0l
float0.0f
double0.0d

 Package day09;  Public class Testinit {        publicstaticvoid  main (string[] args) {        int  Anum;        Anum=20;        System.out.println (Anum);    }}

 Packageday09; Public classDefaultValue {//declaring class variables    Static BooleanAboolean; Static CharAchar;//'/000 '    Static byteAbyte; Static  ShortAshort; Static intaint; Static floatafloat; //declaring instance variables    Doubleadouble;  Public Static voidMain (string[] args) {System.out.println ("Aboolean=" +Aboolean); System.out.println ("Achar=" +Achar); System.out.println ("Abyte=" +abyte); System.out.println ("Aboolean=" +Aboolean); System.out.println ("Ashort=" +ashort); System.out.println ("Aint=" +aint); System.out.println ("Afloat=" +afloat); //System.out.println ("adouble=" +adouble);    }}

Resources

[1] Java Easy Start Classic tutorial "full version"

Java Section No. 09 assignment and initialization

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.