Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaLanguage
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Variables and Data Types == Java distinguishes two datatypes, primitive types and the reference type. The primitive data types are: {| border=1 cellpadding=2 cellspacing=0 |- |boolean |1 byte |true, false | |- |byte |1 byte | | |- |char |2 bytes | |0xxxx - 0xffff |- |double |8 bytes | | |- |float |4 bytes |1.14, 4.23E23, 1.4f | |- |int |4 bytes |0x1c, 0x1C, 0X1c, 034, 28 |2^31 bis (2^31)-1, -2147483648 - 2147483647 |- |long |8 bytes |400000000L,1l, 0xCAFE | |- |short |2 bytes | |2^15 bis (2^15)-1, -32768 - 32767 |- |void | |only for return of a method, even not for an parameter | |} The size and format and therefore the range of the primitive data types are defined by the language specification and so the language is platform independent. Char is a 16-bit Unicode character. Bei mathematischen Operationen wird automatisch (promotion) in den größten erforderlichen Datentyp gewandelt ('''widening'''), aber trotzdem ist gibt bei folgendem Beispiel Datenverlust: class X { void printFloat(float f) { System.out.println(f); } } X x = new X(); x.printFloat(12/5); // da beide Werte integers sind, wird nicht gecastet Das Gegenstück '''narrowing''' ist bis auf die Ausnahme der Zuweisung (nicht Methodenaufrufe) eines konstanten int in byte, short or char nur explizit erlaubt. Ein cast eines floats oder doubles auf ein numerischen Typ enthält ergibt den fractional part des floats bzw. doubles. Man kann nicht von boolean auf einen numerischen Typ casten. Implizite casts sind byte -> short -> int -> long -> float -> double. Ein literal value like "23.4" is considered as double. Example(u) zeigt, wie es zu einem impliziten Datenverlust kommen kann. ===Konvertierung zwischen Datentypen=== Für die Basisdatentypen gibt es sogenannte '''wrapper classes''': *Spezialisierungen von java.lang.number: Integer, Float, Long, ... *Boolean *Character so daß Variablen mit den Objekt-Methoden behandelt werden können wie z.B. in einem Object-Array abgespeichert zu werden. Objekte der wrapper classes sind leider immutable, und eignen sich daher nicht als in-out parameter. In diesem Fall kann man einen Array der Länge 1 verwenden. ===Konvertierungen nach char bzw. Character=== von int to char: c = (char)i von char to Character: new Character(c) ===Konvertierung nach int=== von char to int: i = (int)c; ===Referenz Datentyp=== All other types as arrays, objects or interfaces are references, that is even return values, so be carefully returning objects and check whether you shouldn’t better clone it . There are no structs, pointer or unions. Alle Referenzvariablen also nahezu alle Variablen müssen mit new instanziert werden. C ObjectOfC; ObjectOfC.DoSomething(); // does not compile because no new ObjectOfC = new C(); // this is correct (don’t forget the parantheses) Man kann alle Referenzvariablen explizit auf null setzen ObjectOfC = null; bzw. auf null abfragen. Local Variables are not automatically initialized with null. Am besten ist der Vergleich von Java Referenzvariablen zu Objekt-Pointern von C++. Dies ist ein enorm wichtiger Punkt. Wenn z.B. ein Objekt eine Referenz auf ein anderes Objekt als Methodenparamter erhält und sich dieses Objekt intern als Referenz merkt betreffen alle Änderungen an dem Objekt zu einem späteren Zeitpunkt natürlich auch Verwendungen innerhalb weiterer Methoden . Speichert man beispielsweise in einem Array of Superclasses Subclasses und ruft Methoden auf, so geht werden bei derefernzierten Aufrufen über die Array-Elemente die Subclass Methoden aufgerufen, da im allgemeinen Fall alle Member virtuell sind. You can declare a variable in any scope to be final, including parameters to methods and constructors. The value of a final variable cannot change after it has been initialized. final int aFinalVar = 0; Man kann auch quasi anonyme Variablen anlegen, z.B. beim Aufruf einer Methode, die ein Objekt als Parameter hat. ===Casts=== Man kann immer ein Subclass Objekt auf ein Superclass Objekt ohne expliziten cast casten, die umgekehrte Richtung geht nur mit einem expliziten cast. Geht ein cast schief z.B. weil ein downcast nicht korrekt ist wird eine Exception ausgelöst. Um zu prüfen, ob ein downcast erlaubt ist gibt es den instanceof-Operator der wie folgt eingesetzt wird: if (<VariableName> instanceof <ClassInterfaceName>) <code>If <VariableName> == null</code> dann ist es keine Instanz von <ClassInterfaceName>. Der folgende Ausdruck ist ebenfalls legal und liefert true: if (<VariableName> instanceof Object)
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width