Documents

Java Programming Constructs

Description
this is the publication of Oxward
Categories
Published
of 32
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
   I often say . . . that when you can measure what you are speaking about, and express it in numbers, you know something about it; but when you cannot measure it, when  you cannot express it in numbers, your knowledge is of a meager and unsatisfactory kind; it may be the beginning of knowledge, but you have scarcely, in your thoughts, advanced to the stage of science, whatever the matter may be. Lord Kelvin After reading this chapter, the readers will be able to   understand how variables are used in    know the basic data types    learn expressions and conditional statements    use all the available operations in Java    know the basics of conversion and casting    understand loops and branching statements 3.1 VARIABLES  Variable is a symbolic name refer to a memory location used to store values that can change during the execution of a program. Java declares its variables in the following manner: intnoofwatts=100;// variable declarationData typeIdentifierLiteral A variable declaration involves specifying the type (data type), name (identifier), and value (literal) according to the type of the variable. Let us have a look at the three components in detail. 3.2 PRIMITIVE DATA TYPES Primitive data types are the basic building blocks of any programming language. A primitive data type can have only one value at a time and is the simplest built-in form of data within Java.  Java Programming Constructs   3   Java Programming Constructs   43 All variables in Java have to be declared before they can be used, that is why Java is termed as a  strongly typed language . There are eight primitive data types in Java, as follows: For whole number  byteshortintlongfloatdoublechar  booleanFor real numbersCharactersBooleanJava is portable across computer platforms. C and C++ leave the size of data types to the machine and the compiler, but Java specifies everything. Note  All integer (byte, short, int, long) and floating-point types (float, double) are signed in Java. byte  It is a 1-byte (8-bit) signed 2’s complement integer. It ranges from –128 to 127 (inclusive). The byte data type can be used where the memory savings actually matter. short  It is a 2-byte (16-bit) signed 2’s complement integer. It ranges from –32,768 to 32,767 (inclusive). As with byte , you can use a short to save memory.  int  It is a 4-byte (32-bit) signed 2’s complement integer. It ranges from –2,147,483,648 to 2,147,483,647 (inclusive). For integral values, this data type is the default choice. long  It is an 8-byte (64-bit) signed 2’s complement integer. It ranges from  –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). This data type should be used only when you need a range of values wider than int . Floating point conforms to the IEEE 754-1985 binary fl oating point standard.   fl oat  It is a single-precision 32-bit fl oating point. It ranges from 1.401298464324817e–45f to 3.402823476638528860e+38f.  double  This data type is a double-precision 64-bit floating point. It ranges from 4.94065645841246544e–324 to 1.79769313486231570e (+) 308. For decimal numbers, this data type is the default choice. boolean  It has only two possible values:  true and false . The size of this data type is not  precisely de fi ned. char  The unsigned char  data type is a single 16-bit unicode character. It ranges from ‘\u0000’  (or 0) to ‘\uffff’  (or 65,535 inclusive). Note Unlike C/C++, where handling of character sequences is tedious, Java provides a class named “String” for handling character strings enclosed within double quotes. Although it is not a primitive data type, Java string solves much of the complexity with ease.  44    Programming in Java 3.3 IDENTIFIER Identifiers are names assigned to variables, constants, methods, classes, packages, and interfaces.  No limit has been specified for the length of a variable name. Identifiers can have letters, numbers, underscores, and any currency symbol. However they may only begin with a letter, underscore, or a dollar sign. Digits cannot be the first character in an identifier. 3.3.1 Rules for Naming 1. The fi rst character of an identi fi er must be a letter  , an underscore , or a dollar sign ($). 2. The subsequent characters can be a letter  , an underscore , dollar sign , or a digit  . Note that white spaces  are not allowed within identi fi ers. 3. Identi fi ers are case-sensitive . This means that Total_Price  and total_price  are different identi fi ers. Do not use Java’s reserved keywords . A few examples of legal and illegal identi fi ers are shown below. Legal Identi fi ersIllegal Identi fi ers MyClassMy Class$amount23amount_totalPay-totalpaytotal_Commissiontotal@commission 3.3.2 Naming Convention  Names should be kept according to their usage, as it is meaningful and easy to remember as shown in Fig. 3.1. Class declarationPackage declarationImporting other packages public class HelloWorld Beginning of the classMain method declarationPrint statementEnd of main methodEnd of the class public static void main(args[])package hello;import Java.lang.*;{System.out.println( Hello How are You? );}} Fig. 3.1 Naming Convention Used in Java   Java Programming Constructs   45 Class or Interface Identifiers  These begin with a capital letter. The first alphabet of every internal word is capitalized. All other letters are in lower case. public class MyClass // class identifier: MyClass interface Calculator; // interface identifier: Calculator Variable or Method Identifiers  These start with a lower-case letter. The first alphabet of every internal word is capitalized. All other letters are in lower case. int totalPay; // variable identifier: totalPayMyClass.showResult(); // MyClass is the Class Name and showResult() is a method of MyClass.  Constant Identifiers  These are specified in upper case. Underscores are used to separate internal words. final double TAX_RATE = 0.05; // constant identifier: TAX_RATE Package Identifiers These consist of all lower-case letters. package mypackage.subpackage.subpackage; //Package Declaration 3.3.3 Keywords  Keywords are predefined identifiers meant for a specific purpose and cannot be used for identifying used defined classes, variables, methods, packages, and interfaces. All keywords are in lower case. Table 3.1 lists the keywords in Java. Table 3.1  Keywords in Java abstractassertbooleanbreakbytecasecatchcharclasscontinuedefaultdodoubleelseenumextends  fi nal  fi nally  fl oatfor ifimplementsimportinstanceofintinterfacelongnativenewpackage privateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhileconst*goto**const and goto are reserved keywords. 3.4 LITERALS A literal is a value that can be passed to a variable or constant in a program. Literals can be numeric (for byte, short, int, long, float, double), boolean, character, string notations or null literals.  Numeric Literals  can be represented in binary, decimal, octal, or hexadecimal notations. These literals can be assigned to all numeric types in Java including char (based on their respective range and size).   Binary literals  are a combination of 0’s and 1’s. Binary literals can be assigned to variables in Java 7. Binary literals must be prefixed with 0b or 0B (zerob or zeroB). For example,

Motors Micro Max

Jul 23, 2017
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks