, , , , . :
double, double
, float, float
, long, long
int
1 :
int a = 3;double b = 4.6;double c = a+b;double, double a+b double.
2 :
byte a = 3;short b = 4;byte c = (byte)(a+b);byte short ( double, float long), int, a+b int. byte, byte.
char, int:
int d = 'a' + 5;System.out.println(d); // 102, , int -> float, long -> float long -> double , .
:
float b = 123456789;System.out.println(b); // 1.23456792E8
1. :
+, -, *, /, %, (+, -=, *=,/= ..), , .
2.
|, |=, &, &=, ^,^=,~, <<, >>, <<=, >>=, <<< ( ),>>>.
3.
, , .
>, <, <=, >=, ==,!=
4.
||, &&,!, (?).
. .
:
1)++ (), -- ()
2)* (), / (), % ( )
3)+ (), - ()
:
int a = 8;
int b = 7;
int c = a + 5 * ++b;
System.out.println(c); // 48
++b, - b . 5 * ++b, a + 5 * ++b
:
int a = 8;
int b = 7;
|
|
int c = (a + 5) * ++b;
System.out.println(c); // 104
, :
=
: c=b;
+=
c+=b; ( c c b)
-=
c-=b; ( c b c)
*=
c*=b; ( c c b)
/=
c/=b; ( c c b)
%=
c%=b; ( c c b)
&=
c&=b; ( c c&b)
|=
c|=b; ( c c|b)
^=
c^=b; ( c c^b)
<<=
c<<=b; ( c c<<b)
>>=
c>>=b; ( c c>>b)
>>>=
c>>>=b; ( c c>>>b)
:
int a = 5;
a += 10;// 15
a -= 3;// 12
a *= 2; // 24
a /= 6; // 4
a <<= 4; // 64
a >>= 2; // 16
System.out.println(a); // 16
, , .
12.1.
:
[] __ _ ([])
{
//
}
.
Java main, :
public static void main(String[] args)
{
System.out.println(" !");
}
public static . . void , .
- main - String[] args. - , .
Program: hello() welcome(). , ++.
public class Program
{
public static void main (String args[])
{
}
void hello()
{
System.out.println("Hello");
}
void welcome()
{
System.out.println("Welcome to Java 10");
}
}
: hello welcome, . - Program, main.
12.2.
, . , . Java main . , , , main.
.
:
_();
, - .
, :
|
|
public class Program
{
public static void main (String args[])
{
hello();
welcome();
welcome();
}
static void hello()
{
System.out.println("Hello");
}
static void welcome()
{
System.out.println("Welcome to Java 10");
}
}
main hello welcome. : . , .
, main , main, static.
:
Hello
Welcome to Java 10
Welcome to Java 10
12.3.
, . :
static void sum(int x, int y){
int z = x + y;
System.out.println(z);
}
- , .
, :
public class Program{
public static void main (String args[]){
int a = 6;
int b = 8;
sum(a, b); // 14
sum(3, a); // 9
sum(5, 23); // 28
}
static void sum(int x, int y)
{
int z = x + y;
System.out.println(z);
}
}
1. .
2. : . .. (.. ).
sum int, int. , , int int. , , . , , - .
:
public class Program{
public static void main (String args[]){
display("Tom", 34);
display("Bob", 28);
display("Sam", 23);
}
static void display(String name, int age){
System.out.println(name);
System.out.println(age);
}
}
display . String, - int. , .
12.4.
. , , , - 3, 4, 5 . :
public class Program{
public static void main (String args[]){
sum(1, 2, 3); // 6
sum(1, 2, 3, 4, 5); // 15
sum();// 0
}
static void sum(int...nums){
int result =0;
for(int n: nums)
result += n;
System.out.println(result);
}
}
int...nums , . sum , , . , , :
public static void main(String[] args) {
sum("Welcome!", 20,10);
|
|
sum("Hello World!");
}
static void sum(String message, int...nums){
System.out.println(message);
int result =0;
for(int x:nums)
result+=x;
System.out.println(result);
}
12.5. return
. return.
return _;
return , . , - .
:
public class Program{
public static void main (String args[]){
int x = sum(1, 2, 3);
int y = sum(1, 4, 9);
System.out.println(x); // 6
System.out.println(y); // 14
}
static int sum(int a, int b, int c){
return a + b + c;
}
}
void . sum int, . , void, return .
, . int, return , int. .
return :
public class Program{
public static void main (String args[]){
System.out.println(daytime(7)); // Good morning
System.out.println(daytime(13)); // Good after noon
System.out.println(daytime(18)); // Good evening
System.out.println(daytime(2)); // Good night
}
static String daytime(int hour){
if (hour >24 || hour < 0)
return "Invalid data";
else if(hour > 21 || hour < 6)
return "Good night";
else if(hour >= 15)
return "Good evening";
else if(hour >= 11)
return "Good after noon";
else
return "Good morning";
}
}
12.6.
return , . return , , void:
public class Program{
public static void main (String args[]){
daytime(7); // Good morning
daytime(13); // Good after noon
daytime(32); //
daytime(56); //
daytime(2); // Good night
}
static void daytime(int hour){
if (hour >24 || hour < 0)
return;
if(hour > 21 || hour < 6)
System.out.println("Good night");
else if(hour >= 15)
System.out.println("Good evening");
else if(hour >= 11)
System.out.println("Good after noon");
else
System.out.println("Good morning");
}
}
datetime 24 0, . return .
, / . (method overloading).
:
1
2
3
4
5
6
7
8
9
10
11
12
public class Program{
public static void main(String[] args) {
System.out.println(sum(2, 3)); // 5
System.out.println(sum(4.5, 3.2)); // 7.7
System.out.println(sum(4, 3, 7)); // 14
|
|
}
static int sum(int x, int y){
return x + y;
}
static double sum(double x, double y){
return x + y;
}
static int sum(int x, int y, int z){
return x + y + z;
}
}
sum(), , .
, . . , :
public class Program{
public static void main(String[] args) {
System.out.println(sum(2, 3));
System.out.println(sum(4, 3));
}
static int sum(int x, int y){
return x + y;
}
static double sum(int x, int y){
return x + y;
}
}
. , .
, :
1. expr++ expr-- |
2. ++expr --expr +expr -expr ~! |
3. * / % |
4. + - |
5. << >> >>> |
6. < > <= >= instanceof |
7. ==!= |
8. & |
9. ^ |
10. | |
11. && |
12. || |
13.?: ( ) |
14. = += -= *= /= %= &= ^= |= <<= >>= >>>= ( ) |
, . , .
, . , :
_ _[];
//
_[] _;
, :
int nums[];
int[] nums2;
:
int nums[];
nums = new int[4]; // 4
:
new _[_],
new - , .
, nums = new int[4]; - int .
:
int nums[] = new int[4]; // 4
int[] nums2 = new int[5]; // 5
. ( char) 0, boolean false, null. , int 0, nums .
:
//
int[] nums = new int[] { 1, 2, 3, 5 };
int[] nums2 = { 1, 2, 3, 5 };
, , .
:
int[] nums = new int[4];
nums[0] = 1;
nums[1] = 2;
nums[2] = 4;
nums[3] = 100;
System.out.println(nums[2]); // 4
0, , , nums[3].
4 , , , : nums[5] = 5;. , .
15.1.
, . . - , :
int[] nums1 = new int[] { 0, 1, 2, 3, 4, 5 };
int[][] nums2 = { { 0, 1, 2 }, { 3, 4, 5 } };
:
nums1
0 | 1 | 2 | 3 | 4 | 5 |
nums2
0 | 1 | 2 |
3 | 4 | 5 |
nums2 , . : int[][] nums2 = new int[3][3];. . - . , , :
|
|
//
nums2[1][0]=44;
System.out.println(nums2[1][0]);
:
int[][][] nums3 = new int[2][3][4];
15.2.
" ". 3 , . :
int[][] nums = new int[3][];
nums[0] = new int[2];
nums[1] = new int[3];
nums[2] = new int[5];
15.3.
, , length, , :
int length = nums.length;
, , :
int last = nums[nums.length-1];