Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Primitive Data Primitive Data Last modified Monday, 30-Aug-2021 11:43:47 EDT. Data Values Classes, objects, and methods can contain data values (see The Big Picture) A class data value (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. An instance data value is used to maintain information specific to individual instances. Method data values (including parameters) are local to the method Example: MetricConversion.java Two types of data values (Slides) Primitive variables contain raw data Reference variables point at objects Declarations When a variable declaration is made, memory space is allocated to store the values of the variables. A variable has three properties: A memory location to store the value The type of value stored The name used to refer to the memory location. The syntax for declarations is ; Example: MetricConversion.java Variables have (in principle) garbage in them when declared. Java is kind ... it will zeroize some types of variables, but you should never trust a prankster. Java is kind ... it won't let you use a variable that has not been assigned a value. Geoff's rule ... declare all variables at the start of the class or method. Example: PizzaCost.java Assignment Values are assigned into variables using assignment statements. The syntax for assignments is = ; Example: MetricConversion.java Initialization assignment Variables may initialized by assignment in their declaration. Example: NumericVariables.java For final variables, this is their only assignment Example: MetricConversion.java Class constants Class constants (indicated by the static and final modifiers) contain values that are global to the class, or even the world. Class constants for the class should be private Class constants for the world should be public Example: MetricConversion.java Primitive numeric data values Example: NumericVariables.java Example: NumericOperators.java Example: NumericPrecedence.java Example: NumericCasting.java Example: FinalVariables.java Math class See SUN's documentation Example: MathFunctions.java Primitive Boolean data values The boolean values (after George Boole) are true and false Example: Boolean.java Boolean values are used to control the flow of execution in selection and repetition statements (coming to a class near you soon) Boolean values can be created with relational operators (Slides) Example: MakeBooleans.java Just like mathematical operators, Boolean operators combine Boolean values. Example: CombineBooleans.java Example: ShortCircuit.java Boolean values are used to form conditional expressions, whose value is determined by a Boolean expression. Example: Conditional.java Primitive character data values (Slides) Example: ASCIITranslate.java Characters can be compared Example: CharOrder.java Special characters Special characters are written as escape sequences, which represent single characters by using a \ prefix Name Escape Tab \t Newline \n Backspace \b Single quote \' Backslash \\ Bell (char)7 Example: EscapeCharacters.java Character class See SUN's documentation Example: CharGames.java Example: CharGamesCompressed.java Some rules for safe programming with data values Declare constant data as final Always declare public static data as final (to avoid warts). Declare static variables private in preference to public Always declare non-static variables private to provide encapsulation. The public, private, and static modifiers are not used for local variables Exercises What numeric data type would be most appropriate to store: An amount of money The number part of a UM student number The day number in a year What is the data type of each of the following: 234.567 0L (short) 234 2e2f Why is the result of the following 27? 5+2*11 ? How could the previous expression be modified to make the result 77? What will give you warts? Exam Style Questions Name the six primitive numeric data types in Java. What is the data type of each of these expressions: 2 * 2.0f * 2.0 (long) -2 * 2.0 (byte) 2 * (byte) 2 2 * 2L What is the output from this program [an error occurred while processing this directive] What are the escape characters for: Tab Backspace Backslash Newline How are characters represented in the memory of a computer? What is "short circuit evaluation" of Boolean expressions? What is the output from the following: height = 10; System.out.println("The cat " + (height > 10 ? "flew" : "sat") + " on the\b big\nmat");