You are on page 1of 8

MateriPraktikumPBOPertemuanI

HowJavaWorks

SourceCode(.java) JavaCompiler(javac) Bytecode(.class) JavaVirtualMachine(java)

JavaSourceCodeStructure

publicclassSayHello{ publicstaticvoidmain(String[]args){ System.out.println("Hello!"); } }


1

MateriPraktikumPBOPertemuanI

PrimitiveTypes
TheJavalanguagedefineseightprimitivetypes(alleightofthesetypenamesareJava keywords),asfollows. Fourtypesofintegernumericdata: byte:8bitunsignedinteger short:16bitsignedinteger int:32bitsignedinteger long:64bitsignedinteger Twofloatingpointnumerictypes: float:32bitsingleprecisionfloatingpoint double:64bitdoubleprecisionfloatingpoint Plustwoadditionalprimitivetypes: char:Asinglecharacter,storedusing16bitUnicodeencoding(versus8bitASCII encoding),thatenablesJavatohandleawiderangeofinternationalcharactersets. boolean:Avariablethatmayonlyassumeoneoftwovalues:trueorfalse(bothof thesevaluesarereservedwordsinJava).

MateriPraktikumPBOPertemuanI

IdentifiersandVariableNaming
Identifiersaretokensthatrepresentnamesofvariables,methods,classes,etc.Examples ofidentifiersare:Hello,main,System,out. Javaidentifiersarecasesensitive.Thismeansthattheidentifier:Helloisnotthesame ashello.Identifiersmustbeginwitheitheraletter,anunderscore_,oradollarsign $.Lettersmaybeloweroruppercase.Subsequentcharactersmayusenumbers0to 9. IdentifierscannotuseJavakeywordslikeclass,public,void,etc.Wewilldiscussmore aboutJavakeywordslater. ThefollowingareallvalidvariablenamesinJava: intsimple; //startswithalphabeticcharacter int_under; //startswithunderscore intmore$money_is_2much; //maycontaindollarsigns,and/or //underscores,and/ordigits,and/or //alphabeticcharacters whiletheseareinvalid: int1bad; //inappropriatestartingcharacter intnumber#sign; //containsinvalidcharacter intfoobar; //containsinvalidcharacter intplus+sign; //containsinvalidcharacter intx@y; //containsinvalidcharacter intdot.notation; //containsinvalidcharacter Javaisacasesensitivelanguage.Thatis,theuseofuppercaseversuslowercaseinJava isdeliberateandmandatory,forexample: Variablenamesthatarespelledthesamewaybutthatdifferintheiruseofcase representdifferentvariables: //ThesearetwoDIFFERENTvariablesasfarastheJava //isconcerned. intx;//lowercase intX;//uppercase Allkeywordsarerenderedinlowercase:public,class,int,boolean,andsoforth. Dontgetcreativeaboutcapitalizingthese: //Thereservedword'for'shouldbelowercase. For(inti=0;i<3;i++){

MateriPraktikumPBOPertemuanI

JavaKeywords
KeywordsarepredefinedidentifiersreservedbyJavaforaspecificpurpose.Youcannot usekeywordsasnamesforyourvariables,classes,methodsetc.Hereisalistofthe JavaKeywords.

JavaLiterals
Literalsaretokensthatdonotchangeorareconstant.Thedifferenttypesofliteralsin Javaare:IntegerLiterals,FloatingPointLiterals,BooleanLiterals,CharacterLiteralsand StringLiterals.

IntegerLiterals
Integerliteralscomeindifferentformats:decimal(base10),hexadecimal(base16),and octal(base8).Inusingintegerliteralsinourprogram,wehavetofollowsomespecial notations. Fordecimalnumbers,wehavenospecialnotations.Wejustwriteadecimalnumberas itis.Forhexadecimalnumbers,itshouldbepreceededby0xor0X.Foroctals,they arepreceededby0. For example, consider the number 12. It's decimal representation is 12, while in hexadecimal,itis0xC,andinoctal,itisequivalentto014. Integerliteralsdefaulttothedatatypeint.Anintisasigned32bitvalue.Insomecases, youmaywishtoforceintegerliteraltothedatatypelongbyappendingthelorL character.

FloatingPointLiterals
Floatingpointliteralsrepresentdecimalswithfractionalparts.Anexampleis3.1415. Floatingpointliteralscanbeexpressedinstandardorscientificnotations.Forexample, 583.45isinstandardnotation,while5.8345e2isinscientificnotation. Floatingpointliteralsdefaulttothedatatypedoublewhichisa64bitvalue.Tousea smallerprecision(32bit)float,justappendtheforFcharacter.

MateriPraktikumPBOPertemuanI

BooleanLiterals
Booleanliteralshaveonlytwovalues,trueorfalse.

CharacterLiterals
Character Literals represent single Unicode characters. A Unicode character is a 16bit charactersetthatreplacesthe8bitASCIIcharacterset.Unicodeallowstheinclusionof symbolsandspecialcharactersfromotherlanguages. Touseacharacterliteral,enclosethecharacterinsinglequotedelimiters.Forexample, thelettera,isrepresentedas'a'. Tousespecialcharacterssuchasanewlinecharacter,abackslashisusedfollowedby the character code. For example, '\n' for the newline character, '\r' for the carriage return,'\b'forbackspace.

StringLiterals
String literals represent multiple characters and are enclosed by double quotes. An exampleofastringliteralis,"HelloWorld".

Operators
In Java, there are different types of operators. There are arithmetic operators, relational operators, logical operators and conditional operators. These operators follow a certain kind of precedence so that the compiler will know which operator to evaluate first in case multiple operators are used in one statement.

MateriPraktikumPBOPertemuanI

Logical operators have one or two boolean operands that yield a boolean result. There are six logical operators: && (logical AND), & (boolean logical AND), || (logical OR), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR), and ! (logical NOT). The conditional operator ?: is a ternary operator. This means that it takes in three arguments that together form a conditional expression. The structure of an expression using a conditional operator is,

exp1?exp2:exp3
wherein exp1 is a boolean expression whose result must either be true or false. If exp1 is true, exp2 is the value returned. If it is false, then exp3 is returned.

MateriPraktikumPBOPertemuanI

OperatorPrecedence

MateriPraktikumPBOPertemuanI

Referensi
Balagtas,FlorenceTiu.IntroductiontoProgrammingI.JEDI.2006. Barker,Jacquie.BeginningJavaObjects.Apress.2005.