Showing posts with label Pemrograman Java. Show all posts
Showing posts with label Pemrograman Java. Show all posts

Sunday, June 12, 2011

JAVA : Konstanta Java


download materi konstanta

At Java, we utilize key word (keyword) final to point out konstanta.

example:




lingkaran.java
public class lingkaran {
   public static void main(String args[]) {
       final double PHI = 3.14;
       double radius = 100;
       System.out.println(“keliling lingkaran berjari -             jari”+radius+”adalah”+(2*PHI*radius));
   }
}


Final key word betoken point application goes to variable at do once only, point afterwards at variable it can't at changes eternally.

convention:
Name konstanta is uppercase the lot, e.g.:



final double PHI = 3.14
final double CM_PER_INCHI = 2.54;

Often at Java, konstanta applies to all method / function at one class. Konstanta this ordinary at konstanta's mention brazes (class constant)  We can make konstanta class with static's key word final.

example:




Lingkaran.java
public class lingkaran {
   public static final double PHI = 3.14;
   
   static void test() {
   lingkaran t = new lingkaran();

   double radius = 100;
   System.out.println(“keliling lingkaran berjari - jari”+radius+”adalah”+t.calcCircumference(radius));
   System.out.println(“keliling lingkaran berjari - jari”+radius+”adalah”+t.calcArea(radius));
   }
   public static void main(String args[]){
      test();
   }
   static double calcCircumference(double r){
      return (2*PHI*r);
   }
   static double calcArea(double r){
      return (PHI*r*r);
   }

konstanta's definition at do outside main's method(). Thus, konstanta can at utilizes at other methods at same class.

More again, konstanta at samples at declaration utilizes access modifier well worth public, therefore methods at even other class gets to utilize konstanta by points konstanta's name by calls class name as follows:



<nama kelas>.<nama konstanta>.

On example upon konstanta PHI was pointed to utilize identifier following:



Lingkaran.PHI

download  materi konstanta

JAVA : Keyword and Literal Java


download materi keyword and literal

KEYWORD

keyword is identifier that at utilizes Java for a special aim. keyword's list Java as follows:












Abstract
Double
Int
Super
Boolean
Else
Interface
Switch
Break
Extends
Long
Synchronized
Byte
False
Native
This
Byvalue
Final
New
Threadsafe
Case
Finally
Null
Throw
Catch
Float
Package
Transient
Char
For
Private
True
Class
Goto
Protected
Try
Const
If
Public
Void
Continue
Implements
Return
While
Default
Import
Short
Do
instanceof
static

We may not utilize keyword as identifier.


literal

Bigger writing for variable is important, literal at Java consists:

1. number
2. character
3. string

Number consisting of integer (integer)  number floats (floating - pont)  and boolean. Boolean was looked on by number. boolean's point to true and false at looks on as 1 and 0. Character always point to Unicode's character. string contains character-string.
download  materi keyword and literal

JAVA : Integer Operator Java


download materi integer operator

operating binary integer operator on integer couple. Following is binary integer operator table.














Deskripsi
Operator
Increase
+
Cut back
-
multiple
*
division
/
Rest for
%
Bitwise AND
&
Bitwise OR
|
Bitwise XOR
^
Left-shift
<< 
Right-shift
>> 
Zero-fill-right-shift
>>> 

sum operator, cut back, multiple, and division(+, -, *, /) doing operation as on aritmatika integer.

Highlight that needs at cermati is make the point division operator work with integer transfer. Division operator transfer integer quotient. If division results rest for therefore modulus operator(%) can at utilizes to get rest point for that.

Following is program Arithmetic to point out aritmatika's operator job binary:




Arithmatic.java
package arithmatic;

public class Arithmatic {
   public static void main (String args[]) {
       int x = 17, y = 5;

       System.out.println(“x =” + x);
       System.out.println(“y =” + y);
       System.out.println(“x + y” + (x+y));
       System.out.println(“x - y” + (x-y));
       System.out.println(“x * y” + (x*y));
       System.out.println(“x / y” + (x/y));
       System.out.println(“x % y” + (x%y));
   }
}


Instruction:

javac Arithmetic.java
Arithmetic's Java

Result Programs:

x = 17
y. = 5
x + y. = 22
x y. = 12
x * y. = 85
x / y. = 3
x % y. = 2

Quotient x / y. which is 17 / 5 results 3. modulus Operation x % y. which is 17 % 5 result 2 (which is rest for integer).

matenatika's ala division with zero don't at defines. Java treats this division by throws / result runtime exeception. We will study exeception alone ala.

bitwise AND's operator, OR, and XOR

bitwise and's operator, or, and xor(&, |, and ^) act on individual bit bit integer. This operator sometime beneficent while integers that at utilizes as field's bit.

example:
Number purpose as field's bit is subject to be declaration a group flag binary number. One number int that mempresentasikan can until 32 flag that different because int at deep keeping 32 bits.




Bitwise.java
package bitwise;

public class Bitwise {
   public static void main (String args[]) {
       Int x = 5, y = 6;
       System.out.println(“x =” + x);
       System.out.println(“y =” + y);
       System.out.println(“x & y” + (x&y));
       System.out.println(“x | y” + (x|y));
       System.out.println(“x ^ y” + (x^y));
  }
}


Instruction:

javac Bitwise
Bitwise's Java

Result programs:

x = 5
y. = 6
x & y. = 4
x | y. = 7
x ^ y. = 3

To understand output result, we shall understand representasi int's number in two' s complement.

Shift operator
Looking at samples following:

x<< 3;
y.>> 7;
z.>>> 2;

On first example, variable individual bit bit integer x is angled to left as much three positions. On second example, bit bit of y. at angles to right as much position sevens. On example drding to point out z. at angles as much 2 positions, with give duck's egg on two left utmost positions.

Looking at programs following:




Shift.java
package shift;

public class Shift {
   Public static void main (String args[]){
       Int x = 7;
       System.out.println(“x =” + x);
       System.out.println(“x >> 2 =” + (x >>> 2));
       System.out.println(“x << 1 =” + (x << 1));
       System.out.println(“x >>> 1 =” + (x >>> 1));
   }
}


Instruction:

javac Shift.java
Shift Java

Result Programs:

x = 7
x>> 2 = 1
x<< 1 = 14
x>>> 1 = 3
download  materi integer operator

JAVA : conversion and casting's data type


download  materi conversion and casting's data type

1. type conversion

we are ordinary do point application one type with other type. if two that type kompatibel therefore Java will do self acting conversion.

example:
if we assign value int goes to variable get long's type.

are not all type mutually kompatibel.

example:
no conversion which is defined from double goes to byte.

We make a abode to be still get to get type conversion that don't kompatibel manually, we get ready to take on accordingly. We do casting. We shall know consequence who may happen so we handle it in order not to begets rioting.

2. convert auto type

while one data type at gives to other data type variable therefore Java will do self acting type conversion if pock two following requisites:
1. Two type is kompatibel
2. Fairish intent type greater at source type appeal

While requisite second tesebut is accomplished, convert enlargement will do self acting Java.

example:
int's type always big have which suffice to keep all all point byte that legitimate. For int's variable case is filled with byte, we not necessarily do casting explicit ala.

Convert enlargement will at do on integer and dot number floats that kompatibel one by other. Type numerik not kompatibel with char or boolean. char's type and boolean is not mutually kompatibel.

Java also self acting does to convert type while keep literal integer goes to variable get byte's type, short, or long.

3. Casting

Available our possible wants to thrust conversion one type goes to other type. Process this enforcing at casting's mention. Casting often at requires while function transfer type in contrast to type which required by operation. casting's action at do with place type that at expects in couple sign boxs in at appreciative left that wants at conversion.

Sintaks casting as follows:

 (target-type) value

type's target define type that at wants deep conversion go to value's type.

example:

casting from int goes to byte
int anInt;
byte aByte;

// ...
...
aByte = (byte) anInt;

If integer point is even greater to be appealed range who can keep all byte, therefore Java mereduksi will go to rest for with range from byte.

purpose example:
read's function() at stream's input standard (System.in) transferring int. We get to do casting to int's type becomes char's type before storage:

char c = (char) ystem. in. read();

System.in.read's function() transferring int's point, then done by casting goes to char with (char)  char's result, that result at keeping at variable c gets char's type.

Stored size science important deep determine casting's end product. Are not all casting can save data in advance safe ala. On casting long's type becomes int long's matter 64 bit becomes int's types 32 bits. While is casting as it is done, kompilator cuts 32 long's type bit becomes int 32 bits. If point 32 above bits contain essential information therefore that information is lost. Information can also get lost while we do casting among given different types even its depositor measures with.

example:
Casting is double's number becomes long will remove fraction information so just gets a part double's round even both fairish 64 bits.

Conversion that result truncation's so-called cut.

Following table is casting that at secures its result no that lost.
Tipe Asal
Tipe Tujuan
Byte
Short, char, int, long, float, double
Short
Int, long, float,double
Char
Int, long, float.,double
Int
Long, float, double
Long
Float, double
Float
Double

download materi conversion and casting's data type

Twitter Delicious Facebook Digg Stumbleupon Favorites More