| # | Ruby | Java |
| 1 |
print "Здравствуй, мир!"
|
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Здравствуй, мир!");
}
}
|
| 2 |
readline | readline |
| 3 |
Integer is the basis for the two concrete classes that hold
whole numbers, Bignum and Fixnum.
Bignum objects hold integers outside the range of
Fixnum. Bignum objects are created automatically when
integer calculations would otherwise overflow a Fixnum. When a
calculation involving Bignum objects returns a result that will
fit in a Fixnum, the result is automatically converted.
A Fixnum holds Integer values that can be represented in a
native machine word (minus 1 bit). If any operation on a
Fixnum exceeds this range, the value is automatically converted
to a Bignum.
|
|
Примитивный тип
|
Размер
|
Минимум
|
Максимум
|
Тип оболочки
|
| boolean |
— |
— |
— |
Boolean |
| char |
16-бит |
Unicode 0 |
Unicode 216- 1 |
Character |
| byte |
8-bit |
-128 |
+127 |
Byte |
| short |
16-bit |
-215 |
+215 — 1 |
Short |
| int |
32-bit |
-231 |
+231 — 1 |
Integer |
| long |
64-bit |
-263 |
+263—1 |
Long |
| float |
32-bit |
IEEE754 |
IEEE754 |
Float |
| double |
64-bit |
IEEE754 |
IEEE754 |
Double |
| void |
— |
— |
— |
Void |
|
| 4 |
(128 >> 2 ^ 3) = 35
Performs various operations on the binary representations of the
Fixnum.
| ~ fix |
Invert bits |
| fix |
| |
aNumeric |
Bitwise OR |
| fix |
& |
aNumeric |
Bitwise AND |
| fix |
^ |
aNumeric |
Bitwise EXCLUSIVE OR |
| fix |
<< |
aNumeric |
Left-shift aNumeric bits |
| fix |
>> |
aNumeric |
Right-shift aNumeric
bits (with sign extension) |
128 >> 2 = 32
128 << 2 = 512
2 ^ 2 = 0
2 ^ 22 = 20
|
(128 >> 2 ^ 3) = 35
| ~ | дополнение |
| & | побитовое И |
| | | побитовое ИЛИ |
| ^ | исключающее ИЛИ |
| << | сдвиг влево |
| >> | сдвиг вправо |
| >>> | сдвиг вправо с размножением нуля |
| a | b | ~a | a|b | a^b | a&b |
| 0 | 0 | 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 | 1 |
|
| 5 |
-1 >> 30 = -1
>>> - parse error |
-1 >>> 30 = 3
-1 >> 30 = -1 |
| 6 |
(4 | 3) ^ (2 & 1) = 7
410=1002; 310=112; 210=102; 110=12;
4 | 3 <=> 100 | 011 <=> 111 <=> 7;
2 & 1 <=> 010 & 001 <=> 000 <=> 0;
(4 | 3) ^ (2 & 1) <=> 111 ^ 000 <=> 111 <=> 7
|
| 7 |
(0x12>>2<<3) = 32
0x12 <=> 18 <=> 10010;
10010 >> 2 = 100;
100 << 3 = 100000 <=> 32
|
| 8 |
(3/2<<2*1>>1%2-4^3) = 35
3/2 = 1
2*1 = 2
1%2-4 = -3
1<<2 = 4
4>>-3 = 32
32^3 <=> 100000 ^ 000011 <=> 100011 <=> 35
|
Приоритеты операторов
- ++; --; +, - (унарные плюс и минус); ~; !;
- *; /; %;
- +; -;
- <<; >>; >>>;
- <; <=; >; >=;
- ==; !=;
- &;
- ^;
- |;
- &&;
- ||;
(3/2<<2*1>>1%2-4^3) = 3
3/2 = 1
2*1 = 2
1%2-4 = -3
1<<2 = 4
4>>-3 = 0
0^3 = 3
|
| 9 |
print "Qq"+("Qq"=="Qq"?"1":"0") Qq1 |
Qq1 |
| 10 |
i = 1
case i
when 1 then puts "1"
when 2 then puts "2"
else puts "3"
end
|
public class Run {
public static void main(String[] argv) {
int i = 1;
switch (i) {
case 1 : System.out.println("1"); break;
case 2 : System.out.println("2"); break;
default : System.out.println("3");
}
}
}
|
| 11 |
ceil (нахождение наименьшего целого не меньшего, чем данное),
floor (наибольшее целое, не большее данного),
round (округление до ближайшего целого)
-4.7.floor <=> -5
-4.7.round <=> -5
-4.7.ceil <=> -4
|
Math.floor(-4.7) <=> -5.0
Math.round(-4.7) <=> -5
Math.ceil(-4.7) <=> -4.0
|