Typy danych
Przykłady deklaracji i inicjacji zmiennych:
int liczba1; // deklaracja zmiennej
liczba1 = 3; // inicjacja zmiennej
int liczba2 = 5; // jednoczesna deklaracja i inicjacja zmiennej
double d = 2.33;
float f1 = (float) 2.33; // liczba zmiennopozycyjna zapisana z kropką jest automatycznie traktowana
// jako double - dlatego trzeba "rzutować" (ang. casting) na typ float
float f2 = 2.33f; // alternatywna metoda rzutowania (ang. casting)
boolean b = liczba1 == 3; // true
if( b )
System.out.println( "To prawda." );
b = liczba1 == 4;
if( !b )
System.out.println( "To NIEprawda." );
if( liczba1 == 3 )
System.out.println( "liczba1 to 3" );
FirstJavaProgram, Hello-2,
public class Hello {
public static void main( String[] args ) {
byte _byte = 3; //1 bajt - zakres od -128 - 127
short _short = 3276; //2 bajty - -32 768 - 32 767
int _int = _short/_byte; //4 bajty - -2 147 483 648 - 2 147 483 647
System.out.println( _int );
long _long = (_byte+_short)/_int; //8 bajtów - -2^63 - 2^63-1
//float _float = (_byte+_short)/_int; //4 bajty - ok 6-7 cyfr po przecinku
float _float = 10/7; //bez f liczba jest traktowana jak double
//String tmp1 = String.format( "%.5f", _float );
System.out.println( "---------- println _float: " + String.format( "%.5f", _float ) );
System.out.printf( "---------- printf _float: %.2f\n", _float );
double _double = 77777.8888888f; //8 bajtów - ok.15 cyfr po przecinku
double _double2 = _double;
System.out.println( _double2 + " = _double2 PRZED zmianą _double" );
_double = 222.333;
System.out.println( _double2 + " = _double2 PO zmianie _double" );
//BigInteger
//BigDecimal
char _char; //1 znak - zapis: 'a' lub \u0192
/*
\t - tab
\n - nowa linia
\r - powrót karetki
\" - cudzysłów
\' - apostrof
\\ - backslash
*/
boolean _boolean; // true/false
//String
String str = new String();
String str2 = new String( "AnotherString" );
str.compareTo( str2 );
System.out.println( " byte: " + _byte );
System.out.println( "short: " + _short );
System.out.println( " byte + " + "short: " + _byte+_short );
System.out.println( " byte + " + "_byte: " + _byte+_byte );
System.out.println( "1+1=" + 1+1 );
System.out.println( 1+1 );
System.out.println( _byte+_byte + "=byte+byte" );
System.out.println( _byte+_short + "=byte+short" );
System.out.println( 'a'+'b'+'c' );
System.out.println( "a"+"b"+"c" );
byte _byte = 7; //1 bajt - zakres od -128 - 127
byte _byte2 = _byte;
System.out.println( "_byte2 PRZED: " + _byte2 );
_byte = 3; //1 bajt - zakres od -128 - 127
System.out.println( "_byte2 PO: " + _byte2 );
short _short = 5; //2 bajty - -32 768 - 32 767
int _int = _short/_byte; //4 bajty - -2 147 483 648 - 2 147 483 647
int _int2 = 8;
System.out.println( "_int+_int: " + (_int2 + _int2) );
float _float = (float) _short/_byte;
float _float2 = (float) 9.1234;
String tmp1 = String.format( "%.5f", _float );
System.out.println( "tmp1: " + tmp1 );
System.out.println( "_float: " + _float );
System.out.printf( "printf _float: %.2f --- %.1f\n", _float, _float2 );
//BigInteger
//BigDecimal
char _char; //1 znak - zapis: 'a' lub \u0192
_char = 'B';
System.out.println( "_char: " + _char );
____________________________________________________________________________________________________________