/ W3SCHOOLS

W3schools - Variables

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Java variables

variables are containers for storing data values different type of variables

  • String - store text, surrounded by double quotes

  • int - integers without decimals

  • float - floating point numbers with decimals

  • char - single character surrounded by single quotes

  • boolean - values with two states(true o false)

Declaring(creating) variables

Specify the type and assign it a value

  • Syntax :

    type variableName = value ;

if you assign a new value to existing variable, it will overwrite the previous value

Final variables

add “final” keyword, it will declare the variable as “final” o “constant”, unchangeable

  • final int i = 5; then, i = 20; will generate un error

Declare many variables

To declare more than one variable of the same type use c omma-separated list

int a = 1, b = 2, c = 3;

Display variables

println() method is often used to display variables use the “+” character

System.out.println(Hello + name);

Can use “+” to add a variable to another.

String fullName = firstName + lastName;

For numeric value, “+” works as a methematical operator

Java Identifiers

int minutesPerHour = 60; //Good!
int m = 60; // not easy to understand

variables must be identified with identifiers(unique name)

To use descriptive names in order to create understandable and maintainable

The gernal rules for naming variable

Name can contain letter, digit, underscore, dollar sign

must begin with a letter

should start with lowercase, can’t contain whitespace

can also begin with _ and $

case-sensitive “myVar” ≠ “myvar”

reserved words(like Java keywords) can’t use