Java Environment Setup, Data Types, and Variable Scopes Study Notes on Variables
System Requirements and Environment Setup
- To determine if a system is 32-bit or 64-bit, right-click on "This PC" or enter "Properties". The system type will be listed under the device specifications (e.g., "64-bit operating system"). Most modern systems use 64-bit architectures.
- For a 32-bit OS, download the x86 version of the JDK. For 64-bit systems, download the x64 version.
- Downloading the JDK requires signing up for an Oracle account. A dummy email ID can be used for this purpose.
- The installation process involves downloading the .exe file and following the setup wizard by clicking "Next" through all default configurations.
- Verification of Java Installation:
- Open the Command Prompt.
- Type the command: Java−version.
- If successful, it displays the version (e.g., "Java version 1.8.0").
- If unsuccessful, the error message "Java is not recognized as internal or external command" appears.
- IDE (Integrated Development Environment): An IDE is needed to write Java programs. While IntelliJ is an option, Eclipse is considered highly compatible.
- Eclipse Neon 3 is recommended specifically because newer versions may have compatibility issues with Java version 1.8.
- Choose "Eclipse IDE for Java EE Developers" (approx. 304MB).
- Download the x86_64 link for 64-bit Windows systems.
- Eclipse does not require a standard installation; extract the ZIP file and run the "eclipse" application file.
- Workspace Management: Upon launching, Eclipse prompts for a workspace folder. It is strongly advised to create this folder in a drive other than the C drive (such as the D or E drive). This prevents data loss in the event of a system format or crash.
Java Fundamentals and Naming Conventions
- Java Characteristics: Java is an object-oriented, class-based programming language. All code must reside within a class.
- Five Elements of a Java Class:
- Variables
- Methods
- Constructors
- Instance blocks
- Static blocks
- Naming Conventions:
- Class/Interface: Must start with an uppercase letter. Every inner word must also start with an uppercase letter (PascalCase). Example: String, StringBuilder.
- Variable/Method: Must start with a lowercase letter. Every inner word must start with an uppercase letter (camelCase). Example: ageID, employeeName, click(), sendTheKeys().
- Package: All letters must be in lowercase. Example: pages, utility.
- Constant: All letters must be in uppercase. Example: PRIORITY.
Data Types in Java
- Data types are required to specify the type of value a variable will hold at the time of declaration. There are two main categories: Primitive and Non-Primitive.
- Non-Primitive Data Types: Also known as reference or object data types. Examples include classes like String or custom objects created using the new keyword.
- Primitive Data Types: There are 8 primitive data types, divided into Numeric and Non-Numeric categories.
- Numeric - Integral (Whole Numbers):
- byte: Size is 1byte (8bits). Range: −128 to 127. Formula: −2n−1 to 2n−1−1.
- short: Size is 2bytes (16bits).
- int: Size is 4bytes (32bits). Default value is 0.
- long: Size is 8bytes (64bits).
- Numeric - Floating Point (Decimals):
- float: Size is 4bytes. Default value is 0.0.
- double: Size is 8bytes. Default value is 0.0.
- Non-Numeric:
- char: Size is 2bytes. Used for single characters. Default value is a single space.
- boolean: No specific size. Only two values: true or false. Default value is false.
Variable Types and Scopes
- Variable Declaration Syntax: [Modifier]DataTypevariableName=value; (where modifier and initial value are optional; the semicolon is the "statement determiner").
- 1. Local Variables:
- Declared inside a method, constructor, or block.
- Scope is restricted to that specific block.
- Access modifiers (public/private) are not applicable, except for final.
- JVM does not provide default values; they must be initialized before use, otherwise a compile-time error occurs.
- 2. Instance Variables:
- Declared inside the class but outside any method/constructor, without the static keyword.
- Bounded to objects. Memory is allocated whenever an object (new) is created.
- Every object gets its own separate copy of instance variables.
- JVM provides default values (e.g., 0 for int, null for String).
- Modifiers are applicable.
- 3. Static Variables:
- Declared inside the class with the static keyword.
- Also called "Class Level Variables."
- Memory is allocated only once when the .class file is loaded.
- They are shared across all objects of the class.
- Best practice is to access them using the class name (ClassName.variableName).
Methods and Memory Areas
- Method Syntax: [Modifier]ReturnTypemethodName([ParameterList])[throwsException]{…}
- Types of Methods:
- Instance Method: Defined without the static keyword.
- Static Method: Defined with the static keyword. The main method is a prime example of a static method.
- Access Rules:
- Static Area (e.g., main method): To access instance variables or methods here, you must create an object (ClassNameobj=newClassName();).
- Instance Area: Can access both instance and static members directly without creating an object.
- Object Creation Syntax: ClassNameobjName=newClassName();
- In Selenium Framework: Common actions like clicking or typing are often defined as static methods in utility classes so they can be accessed globally using the class name without instantiation.
Questions & Discussion
- Question: What if I don't have a D drive or E drive on my system, only a C drive?
- Response: It is recommended to contact the IMT (IT) team to partition the C drive into multiple drives. If the system ever needs a format, all data on the C drive is lost. Having a separate partition protects your workspace and project data.
- Variable Memory Behavior Exercise:
- If an instance variable ID is set to 10 in the class, and then modified to 20 using obj1.ID, a newly created obj2.ID will still show the original value of 10. This is because each object maintains its own unique memory for instance variables.
- Conversely, a change to a static variable affects the value for the entire class and all its instances.