Difference between revisions of "Java: Berbagai Istilah"
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 50: | Line 50: | ||
o.hashCode() Returns a unique ID o.toString() | o.hashCode() Returns a unique ID o.toString() | ||
− | + | ||
− | + | ==Variable== | |
+ | |||
+ | ===Variable=== | ||
Variable can either be a primitive or a reference variable. A primitive variable contains value while the reference variable contains a reference (pointer) to the object. Hence if you compare two reference variables then you compare if both point to the same object. To compare objects use object1.equals(object2). | Variable can either be a primitive or a reference variable. A primitive variable contains value while the reference variable contains a reference (pointer) to the object. Hence if you compare two reference variables then you compare if both point to the same object. To compare objects use object1.equals(object2). | ||
− | + | ||
+ | ===Instance Variable=== | ||
Instance variable is associated with an instance of the class (also called object). Access works over these objects. | Instance variable is associated with an instance of the class (also called object). Access works over these objects. | ||
Instance variables can have any access control and can be marked final or transisient. Instance variables marked as final can not be changed after assigned to a value. | Instance variables can have any access control and can be marked final or transisient. Instance variables marked as final can not be changed after assigned to a value. | ||
− | + | ||
+ | ===Local Variable=== | ||
Local (stack) variable declarations cannot have access modifiers. | Local (stack) variable declarations cannot have access modifiers. | ||
Line 66: | Line 70: | ||
Local variables don't get default values, so they must be initialized before use. | Local variables don't get default values, so they must be initialized before use. | ||
− | + | ||
− | + | ==Modifier== | |
+ | |||
+ | ===Access Modifier=== | ||
There are three access modifiers: public, protected and private | There are three access modifiers: public, protected and private | ||
Line 74: | Line 80: | ||
protected and default are similar. Only difference: A protected class can get accessed from the package and sub-classes outside the package while a default class can get only access via the same package protected = package plus kids | protected and default are similar. Only difference: A protected class can get accessed from the package and sub-classes outside the package while a default class can get only access via the same package protected = package plus kids | ||
− | + | ||
+ | ===Other modifiers=== | ||
final methods: cannot be overwritten in a subclass | final methods: cannot be overwritten in a subclass | ||
Line 85: | Line 92: | ||
stricttp: class or method | stricttp: class or method | ||
− | + | ||
+ | ==Interface== | ||
Within java an interface is a type just as a class is a type. Like a class an interface defines methods. | Within java an interface is a type just as a class is a type. Like a class an interface defines methods. | ||
Line 96: | Line 104: | ||
Interfaces can have constants which are always implicitly public, static and final. | Interfaces can have constants which are always implicitly public, static and final. | ||
− | + | ||
− | + | ==Additional Definitions== | |
+ | |||
+ | ===Method=== | ||
A method can trigger a certain action. | A method can trigger a certain action. | ||
Line 104: | Line 114: | ||
Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method. | Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method. | ||
− | + | ||
+ | ===Constructor=== | ||
classname (Parameter p1, ..) {} | classname (Parameter p1, ..) {} | ||
The constructor is always called if the class is created. If no explicit constructor is defined the compiler adds implicitly a constructor. If the class is sub-classed then the constructor of the super class is always implicitly called. | The constructor is always called if the class is created. If no explicit constructor is defined the compiler adds implicitly a constructor. If the class is sub-classed then the constructor of the super class is always implicitly called. | ||
− | + | ||
+ | ===Class method or class variable=== | ||
Class method / variable is associated with the class and not the instance. To refer to it use the classname and the class method / variable together with a period ("."). Example System.out.println("Hello World"). The runtime environment associates one class variable for a class no matter how many instances exists. | Class method / variable is associated with the class and not the instance. To refer to it use the classname and the class method / variable together with a period ("."). Example System.out.println("Hello World"). The runtime environment associates one class variable for a class no matter how many instances exists. | ||
Line 118: | Line 130: | ||
The static variable is the same of all instances of the class (global variable) | The static variable is the same of all instances of the class (global variable) | ||
− | + | ||
+ | ===Abstract class=== | ||
If a class has one method which only contain the declaration of the method but not the implementation then this class is abstract and can not be initialized. Sub-classes need then to define the methods except if they also declared as abstract. method abstract double returnDouble(); | If a class has one method which only contain the declaration of the method but not the implementation then this class is abstract and can not be initialized. Sub-classes need then to define the methods except if they also declared as abstract. method abstract double returnDouble(); |
Revision as of 05:56, 7 September 2011
Sumber: http://www.vogella.de/articles/JavaTerminology/article.html
Tulisan ini menjelaskan berbagai istilah yang sering digunakan di Java
Definisi
Sangat penting bagi kita untuk mengerti struktur dasar dari package, class dan object.
Dasar: Package, Class dan Object
Package
Java mengelompokan class menjadi package-package fungsional. Semua class di java.lang akan secara automatis tersedia di java program.
Tujuan utama akan adanya package adalah untuk menghindari adanya tabrakan nama. Seorang programmer mungkin akan membuat java class Test. Programmer lain mungkin akan membuat class dengan nama yang sama. Dengan menggunakan package kita dapat memberitahukan system class yang mana yang akan di panggil. Contoh, jika programme1 meletakan class Test di package programmer1 dan programme2 meletakan class-nya di package "xmlreader" maka kita dapat mengakses class ke dua tersebut melalui xmlreader.Test.
Salah satu alasan penting lainnya adalah melalui package kita dapat mengelompokan class kita menjadi unit logik.
Class
Defini: Sebuah template yang menjelaskan data dan perilaku yang berkaitkan dengan instance dari class tersebut.
Data dari dengan sebuah class di simpan dalam variable; perilaku dari sebuah class atau object di implementasikan dalam method.
Semua dapat di lihat dalam blueprint dari object.
Sebuah class
- Di definisikan dengan kata kunci class.
- Di mulai dengan huruf besar.
- Badan class di tutup dengan {}
Object
Definisi: Sebuah object adalah sebuah instance dari sebuah class.
Sebuah class dapat dilihat sebagai blueprint untuk data dan perilaku sebuah object. Object itu sendiri adalah elemen sesungguhnya yang mempunyai data dan dapat melakukan tindakan.
Contoh, class mungkin blueprint dari sebuah car, sebuah object adalah car yang sesungguhnya.
Setiap object
Every object extends implicitly the Object “Object” and get wherefore gets the methods defined by class Object
o.equals(o1) Is the Object equal to o1
o.getClass() Returns the class of o
o.hashCode() Returns a unique ID o.toString()
Variable
Variable
Variable can either be a primitive or a reference variable. A primitive variable contains value while the reference variable contains a reference (pointer) to the object. Hence if you compare two reference variables then you compare if both point to the same object. To compare objects use object1.equals(object2).
Instance Variable
Instance variable is associated with an instance of the class (also called object). Access works over these objects.
Instance variables can have any access control and can be marked final or transisient. Instance variables marked as final can not be changed after assigned to a value.
Local Variable
Local (stack) variable declarations cannot have access modifiers.
final is the only modifier available to local variables
Local variables don't get default values, so they must be initialized before use.
Modifier
Access Modifier
There are three access modifiers: public, protected and private
There are four access levels: public, protected, default and private
protected and default are similar. Only difference: A protected class can get accessed from the package and sub-classes outside the package while a default class can get only access via the same package protected = package plus kids
Other modifiers
final methods: cannot be overwritten in a subclass
abstractmethod: no method body
synchronized method: threat safe, can be final and have any access control
native methods: platform dependent code, apply only to methods
stricttp: class or method
Interface
Within java an interface is a type just as a class is a type. Like a class an interface defines methods.
Interfaces are contracts for what a class can do but they say nothing about the way in which the class must do it.
Interfaces are per default public and abstract – explicit declaration of these modifiers is optional.
An interface can also have abstract methods, no concrete methods are allowed.
Interfaces can have constants which are always implicitly public, static and final.
Additional Definitions
Method
A method can trigger a certain action.
Method with var-args: Method declares a parameter which accepts from zero to many arguments (syntax: type .. name;) A method can only have one var-args parameter and this must be last parameter in the method.
Overwrite of a superclass method: A method must be of the exact same return parameter and the same arguments. Also the return parameter must be the same. Overload methods: An overloaded method is a method with the same name, but different arguments. The return type can not be used to overload a method.
Constructor
classname (Parameter p1, ..) {}
The constructor is always called if the class is created. If no explicit constructor is defined the compiler adds implicitly a constructor. If the class is sub-classed then the constructor of the super class is always implicitly called.
Class method or class variable
Class method / variable is associated with the class and not the instance. To refer to it use the classname and the class method / variable together with a period ("."). Example System.out.println("Hello World"). The runtime environment associates one class variable for a class no matter how many instances exists.
Class method / variant does exists once for all instances. They are indicated with the word static. If a variable should be existing for all instances and be non-changeable (=CONSTANT) then also the word final is used.
The static method runs without any instance of the class. Does not depend on instance (non-static) variables. Can not use the this object or an instance variables.
The static variable is the same of all instances of the class (global variable)
Abstract class
If a class has one method which only contain the declaration of the method but not the implementation then this class is abstract and can not be initialized. Sub-classes need then to define the methods except if they also declared as abstract. method abstract double returnDouble();
If a class contains an abstract method it also needs to get defined as abstract.