변수명은 숫자로 시작할 수 없다.
_와 $ 이외의 특수 문자는 사용할 수 없다.
int, class, return 등 자바의 키워드는 변수명으로 사용할 수 없다.
int x;
double radius;
char a;
x=1;
radius=2.0;
a='A';
int x=1;
double radius=2.0;
char a='A';
int r=2;
int area=r*r;
System.out.println(area)
//결과
//4
final 키워드는 상수를 만들 때 사용한다.
final int r=2;
(참고로 char도 원시데이터 타입임)
byte: 8 bits
short: 16 bits
int: 32 bits
long: 64 bits
float: 32 bits
double: 64 bits
자바에서 리터럴이란 문자가 가리키는 값 그 자체를 의미한다. 쉽게 생각하면 값과 비슷하다.
+, -, *, /, % 가 있다.
a+=1;
a=a+1;
// 이 두 식은 같다.
a++;//연산 후에 a값 증가
++a;//연산 전에 a값 증가
//둘 다 a의 값을 1 올리는 것은 맞지만 연산 전에 a를 증가시키는지, 연산 후에 a를 증가시키는지가 다르다
int a=5;
int b=4;
double c;
c=a/b;
/*이렇게 되면 c에는 1.25가 아닌 1.0의 값이 들어가게 된다.
a,b 모두 int형이기 때문에 a/b의 계산값도 int형으로, 1이 된다.
따라서 c에 1.25를 넣고 싶다면 아래와 같이 하면 된다.*/
c=(double)a/b;
int a=1;
long b=2;
int a=b; //wrong
long b=a;//fine
char letter='A';
boolean lighton = true;
boolean lightoff = false;
boolean a=(1>2);//false
//참고로 c언어와 다르게 0을 false, 그외에 true라고 받아들이지 않는다.
//따라서 true, false 라고 정확히 해야한다.
### boolean operators
! not
&& and
|| or
^ xor (둘이 다르면 true)
var++, var--
+, - (Unary plus and minus), ++var,--var
(type) Casting
! (Not)
*, /, % (Multiplication, division, and modulus)
+, - (Binary addition and subtraction)
<, <=, >, >= (Comparison)
==, !=; (Equality)
&& (Conditional AND) Short-circuit AND
|| (Conditional OR) Short-circuit OR
=, +=, -=, *=, /=, %= (Assignment operator)
import javax.swing.JOptionPane; 필요함.
String string = JOptionPane.showInputDialog(
null, "Prompt Message", "Dialog Title",
JOptionPane.QUESTION_MESSAGE);
string input을 받고 int type으로 변환하는 법
int intValue = Integer.parseInt(integerAsString);
public class practice {
public static void main(String[] args){
System.out.println("Hello java!");
}
}
//End of line style
public class practice
{
public static void main(String[] args)
{
System.out.println("Hello java!");
}
}
// Next line style