mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-09-25 22:09:00 +02:00
Update java.html.markdown
This commit is contained in:
@@ -1,177 +1,175 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
language: java
|
language: java
|
||||||
|
|
||||||
author: Jake Prather
|
author: Jake Prather
|
||||||
|
|
||||||
author_url: http://github.com/JakeHP
|
author_url: http://github.com/JakeHP
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Java is a general-purpose, concurrent, class-based, object-oriented computer programming language.
|
Java is a general-purpose, concurrent, class-based, object-oriented computer programming language.
|
||||||
Read more here: https://en.wikipedia.org/wiki/Java_(programming_language)
|
Read more here: https://en.wikipedia.org/wiki/Java_(programming_language)
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Single-line comments start with //
|
///////////////////////////////////////
|
||||||
/*
|
// General
|
||||||
Multi-line comments look like this.
|
///////////////////////////////////////
|
||||||
*/
|
// Single-line comments start with //
|
||||||
|
/*
|
||||||
// Import Packages
|
Multi-line comments look like this.
|
||||||
import java.util.ArrayList;
|
*/
|
||||||
import package.path.here;
|
|
||||||
// Import "sub-packages"
|
// Import Packages
|
||||||
import java.lang.Math.*;
|
import java.util.ArrayList;
|
||||||
|
import package.path.here;
|
||||||
// Your program's entry point is a function called main
|
// Import all "sub-packages"
|
||||||
public class Main
|
import java.lang.Math.*;
|
||||||
{
|
|
||||||
public static void main (String[] args) throws java.lang.Exception
|
// Your program's entry point is a function called main
|
||||||
|
public class Main
|
||||||
{
|
{
|
||||||
//stuff here
|
public static void main (String[] args) throws java.lang.Exception
|
||||||
|
{
|
||||||
|
//stuff here
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// Printing, and forcing a new line on next print = println()
|
||||||
// Printing
|
System.out.println("Hello World");
|
||||||
System.out.println("Hello World");
|
System.out.println("Integer: "+10+"Double: "+3.14+ "Boolean: "+true);
|
||||||
System.out.println("Integer: "+10+"Double: "+3.14+ "Boolean: "+true);
|
// Printing, without forcing a new line on next print = print()
|
||||||
|
System.out.print("Hello World");
|
||||||
|
System.out.print("Integer: "+10+"Double: "+3.14+ "Boolean: "+true);
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Types
|
// Types
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
// Byte - 8-bit signed two's complement integer (-128 <= byte <= 127)
|
// Byte - 8-bit signed two's complement integer (-128 <= byte <= 127)
|
||||||
|
byte foo = 100;
|
||||||
// Short - 16-bit signed two's complement integer (-32,768 <= short <= 32,767)
|
|
||||||
|
// Short - 16-bit signed two's complement integer (-32,768 <= short <= 32,767)
|
||||||
//Integer - 32-bit signed two's complement integer (-2,147,483,648 <= int <= 2,147,483,647)
|
short bar = 10000;
|
||||||
int x = 1;
|
|
||||||
|
//Integer - 32-bit signed two's complement integer (-2,147,483,648 <= int <= 2,147,483,647)
|
||||||
//Long - 64-bit signed two's complement integer (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
|
int foo = 1;
|
||||||
|
|
||||||
//Float - Single-precision 32-bit IEEE 754 Floating Point
|
//Long - 64-bit signed two's complement integer (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807)
|
||||||
|
long bar = 100000L;
|
||||||
//Double - Double-precision 64-bit IEEE 754 Floating Point
|
|
||||||
|
//Float - Single-precision 32-bit IEEE 754 Floating Point
|
||||||
//Boolean - True & False
|
float foo = 234.5f;
|
||||||
|
|
||||||
//Char - A single 16-bit Unicode character
|
//Double - Double-precision 64-bit IEEE 754 Floating Point
|
||||||
|
double bar = 123.4;
|
||||||
|
|
||||||
// Other than char, which is always 1 byte, these types vary in size depending
|
//Boolean - True & False
|
||||||
// on your machine. sizeof(T) gives you the size of a variable with type T in
|
boolean foo = true;
|
||||||
// bytes so you can express the size of these types in a portable way.
|
boolean bar = false;
|
||||||
// For example,
|
|
||||||
printf("%d\n", sizeof(int)); // => 4 (on machines with 4-byte words)
|
//Char - A single 16-bit Unicode character
|
||||||
|
char foo = 'A';
|
||||||
// Arrays must be initialized with a concrete size.
|
|
||||||
char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes
|
//Strings
|
||||||
int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes
|
String foo = "Hello World!";
|
||||||
// (assuming 4-byte words)
|
// \n is an escaped character that starts a new line
|
||||||
|
String foo = "Hello World!\nLine2!";
|
||||||
|
System.out.println(foo);
|
||||||
// You can initialize an array to 0 thusly:
|
//Hello World!
|
||||||
char my_array[20] = {0};
|
//Line2!
|
||||||
|
|
||||||
// Indexing an array is like other languages -- or,
|
//Arrays
|
||||||
// rather, other languages are like C
|
//The array size must be decided upon declaration
|
||||||
my_array[0]; // => 0
|
//The format for declaring an array is follows:
|
||||||
|
//<datatype> [] <var name> = new <datatype>[<array size>];
|
||||||
// Arrays are mutable; it's just memory!
|
int [] array = new int[10];
|
||||||
my_array[1] = 2;
|
String [] array = new String[1];
|
||||||
printf("%d\n", my_array[1]); // => 2
|
boolean [] array = new boolean[100];
|
||||||
|
|
||||||
// Strings are just arrays of chars terminated by a NUL (0x00) byte,
|
// Indexing an array - Accessing an element
|
||||||
// represented in strings as the special character '\0'.
|
array[0];
|
||||||
// (We don't have to include the NUL byte in string literals; the compiler
|
|
||||||
// inserts it at the end of the array for us.)
|
// Arrays are mutable; it's just memory!
|
||||||
char a_string[20] = "This is a string";
|
array[1] = 1;
|
||||||
printf("%s\n", a_string); // %s formats a string
|
System.out.println(array[1]); // => 1
|
||||||
|
array[1] = 2;
|
||||||
/*
|
printf("%d\n", my_array[1]); // => 2
|
||||||
You may have noticed that a_string is only 16 chars long.
|
|
||||||
Char #17 is the NUL byte.
|
//Others to check out
|
||||||
Chars #18, 19 and 20 have undefined values.
|
//ArrayLists - Like arrays except more functionality is offered, and the size is mutable
|
||||||
*/
|
//LinkedLists
|
||||||
|
//Maps
|
||||||
printf("%d\n", a_string[16]); => 0
|
//HashMaps
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Operators
|
// Operators
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
int i1 = 1, i2 = 2; // Shorthand for multiple declaration
|
int i1 = 1, i2 = 2; // Shorthand for multiple declarations
|
||||||
float f1 = 1.0, f2 = 2.0;
|
|
||||||
|
// Arithmetic is straightforward
|
||||||
// Arithmetic is straightforward
|
i1 + i2; // => 3
|
||||||
i1 + i2; // => 3
|
i2 - i1; // => 1
|
||||||
i2 - i1; // => 1
|
i2 * i1; // => 2
|
||||||
i2 * i1; // => 2
|
i1 / i2; // => 0 (0.5, but truncated towards 0)
|
||||||
i1 / i2; // => 0 (0.5, but truncated towards 0)
|
|
||||||
|
// Modulo
|
||||||
f1 / f2; // => 0.5, plus or minus epsilon
|
11 % 3; // => 2
|
||||||
|
|
||||||
// Modulo is there as well
|
// Comparison operators
|
||||||
11 % 3; // => 2
|
3 == 2; // => 0 (false)
|
||||||
|
3 != 2; // => 1 (true)
|
||||||
// Comparison operators are probably familiar, but
|
3 > 2; // => 1
|
||||||
// there is no boolean type in c. We use ints instead.
|
3 < 2; // => 0
|
||||||
// 0 is false, anything else is true. (The comparison
|
2 <= 2; // => 1
|
||||||
// operators always return 0 or 1.)
|
2 >= 2; // => 1
|
||||||
3 == 2; // => 0 (false)
|
|
||||||
3 != 2; // => 1 (true)
|
// Bitwise operators!
|
||||||
3 > 2; // => 1
|
~ Unary bitwise complement
|
||||||
3 < 2; // => 0
|
<< Signed left shift
|
||||||
2 <= 2; // => 1
|
>> Signed right shift
|
||||||
2 >= 2; // => 1
|
>>> Unsigned right shift
|
||||||
|
& Bitwise AND
|
||||||
// Logic works on ints
|
^ Bitwise exclusive OR
|
||||||
!3; // => 0 (Logical not)
|
| Bitwise inclusive OR
|
||||||
!0; // => 1
|
|
||||||
1 && 1; // => 1 (Logical and)
|
|
||||||
0 && 1; // => 0
|
|
||||||
0 || 1; // => 1 (Logical or)
|
|
||||||
0 || 0; // => 0
|
|
||||||
|
|
||||||
// Bitwise operators!
|
|
||||||
~0x0F; // => 0xF0 (bitwise negation)
|
|
||||||
0x0F & 0xF0; // => 0x00 (bitwise AND)
|
|
||||||
0x0F | 0xF0; // => 0xFF (bitwise OR)
|
|
||||||
0x04 ^ 0x0F; // => 0x0B (bitwise XOR)
|
|
||||||
0x01 << 1; // => 0x02 (bitwise left shift (by 1))
|
|
||||||
0x02 >> 1; // => 0x01 (bitwise right shift (by 1))
|
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Control Structures
|
// Control Structures
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
if (0) {
|
if (false) {
|
||||||
printf("I am never run\n");
|
System.out.println("I never run");
|
||||||
} else if (0) {
|
} else if (false) {
|
||||||
printf("I am also never run\n");
|
System.out.println("I am also never run");
|
||||||
} else {
|
} else {
|
||||||
printf("I print\n");
|
System.out.println("I print");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// While loops exist
|
// While loops exist
|
||||||
int ii = 0;
|
int ii = 0;
|
||||||
while (ii < 10) {
|
while (ii < 10) {
|
||||||
printf("%d, ", ii++); // ii++ increments ii in-place, after using its value.
|
printf("%d, ", ii++); // ii++ increments ii in-place, after using its value.
|
||||||
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
int kk = 0;
|
int kk = 0;
|
||||||
do {
|
do {
|
||||||
printf("%d, ", kk);
|
printf("%d, ", kk);
|
||||||
} while (++kk < 10); // ++kk increments kk in-place, before using its value
|
} while (++kk < 10); // ++kk increments kk in-place, before using its value
|
||||||
// => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
// => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// For loops too
|
// For loops too
|
||||||
int jj;
|
int jj;
|
||||||
for (jj=0; jj < 10; jj++) {
|
for (jj=0; jj < 10; jj++) {
|
||||||
printf("%d, ", jj);
|
printf("%d, ", jj);
|
||||||
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
} // => prints "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Typecasting
|
// Typecasting
|
||||||
|
Reference in New Issue
Block a user