The note introduces the fundamentals of the Java programming language: data types, arithmetic, and operators.
Java notes of open courses @Codecademy.
Brief Introduction
Java is a programming language designed to build secure, powerful applications that run across multiple operating systems.
The Java language is known to be flexible, scalable, and maintainable.
Data Types
-
int
:-
int
is short for integer, which are all positive and negative numbers, including zero. - The
int
data type only allows values between -2,147,483,648 and 2,147,483,647 (32 bits in binary representation).
-
-
boolean
:- A
boolean
is a data type that can only be eithertrue
orfalse
. (Unlike in Python,True
orFalse
.)
- A
-
char
:-
char
is short for character and can represent a single character. - All
char
values must be enclosed in single quotes, like this:'G'
.
-
-
Variables
- A variable stores a value.
- In Java, all variables must have a specified data type.
Tips: A semicolon
;
is also used to end all Java single code statements. We will cover statements that should not end in a semicolon later in this course. (Python codes don't need so.)
-
Whitespace
- Whitespace is one or more characters (such as a space, tab, enter, or return) that do not produce a visible mark or text. Whitespace is often used to make code visually presentable.
- Java will ignore whitespace in code, but it is important to know how to use whitespace to structure code well.
-
Comments
- A comment is text you want Java to ignore.
-
Single line comments are one line comments that begin with two forward slashes:
//
. -
Multi-line comments are generally longer comments that can span multiple lines. They begin with
/*
and end with*/
.
Arithmetic
-
+
: add -
-
: subtract -
*
: multiply -
/
: divide -
%
: modulo, which returns the remainder of dividing two numbers
Operators
-
Relational operators
- Relational operators compare data types that have a defined ordering, like numbers (since numbers are either smaller or larger than other numbers).
- Relational operators will always return a boolean value of
true
orfalse
.
-
Equality operators
- Equality operators are used to test equality.
- Equality operators do not require that operands share the same ordering.
-
Operators:
-<
: less than.
-<=
: less than or equal to.
->
: greater than.
->=
: greater than or equal to.
-==
: equal to
-!=
: not equal to
网友评论