Java Environment Setup, Data Types, and Variable Scopes Study Notes on Variables

System Requirements and Environment Setup

  • To determine if a system is 3232-bit or 6464-bit, right-click on "This PC" or enter "Properties". The system type will be listed under the device specifications (e.g., "6464-bit operating system"). Most modern systems use 6464-bit architectures.
  • For a 3232-bit OS, download the x8686 version of the JDK. For 6464-bit systems, download the x6464 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.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: JavaversionJava -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.81.8.
    • Choose "Eclipse IDE for Java EE Developers" (approx. 304MB304\,MB).
    • Download the x86_64x86\_64 link for 6464-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: StringString, StringBuilderStringBuilder.
    • Variable/Method: Must start with a lowercase letter. Every inner word must start with an uppercase letter (camelCase). Example: ageIDageID, employeeNameemployeeName, click()click(), sendTheKeys()sendTheKeys().
    • Package: All letters must be in lowercase. Example: pagespages, utilityutility.
    • Constant: All letters must be in uppercase. Example: PRIORITYPRIORITY.

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 StringString or custom objects created using the newnew keyword.
  • Primitive Data Types: There are 88 primitive data types, divided into Numeric and Non-Numeric categories.
    • Numeric - Integral (Whole Numbers):
    • byte: Size is 1byte1\,byte (8bits8\,bits). Range: 128-128 to 127127. Formula: 2n1-2^{n-1} to 2n112^{n-1}-1.
    • short: Size is 2bytes2\,bytes (16bits16\,bits).
    • int: Size is 4bytes4\,bytes (32bits32\,bits). Default value is 00.
    • long: Size is 8bytes8\,bytes (64bits64\,bits).
    • Numeric - Floating Point (Decimals):
    • float: Size is 4bytes4\,bytes. Default value is 0.00.0.
    • double: Size is 8bytes8\,bytes. Default value is 0.00.0.
    • Non-Numeric:
    • char: Size is 2bytes2\,bytes. Used for single characters. Default value is a single space.
    • boolean: No specific size. Only two values: truetrue or falsefalse. Default value is falsefalse.

Variable Types and Scopes

  • Variable Declaration Syntax: [Modifier]DataTypevariableName=value;[Modifier]\,DataType\,variableName = 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 finalfinal.
    • 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 staticstatic keyword.
    • Bounded to objects. Memory is allocated whenever an object (newnew) is created.
    • Every object gets its own separate copy of instance variables.
    • JVM provides default values (e.g., 00 for int, nullnull for String).
    • Modifiers are applicable.
  • 3. Static Variables:
    • Declared inside the class with the staticstatic keyword.
    • Also called "Class Level Variables."
    • Memory is allocated only once when the .class.class file is loaded.
    • They are shared across all objects of the class.
    • Best practice is to access them using the class name (ClassName.variableNameClassName.variableName).

Methods and Memory Areas

  • Method Syntax: [Modifier]ReturnTypemethodName([ParameterList])[throwsException]{}[Modifier]\,ReturnType\,methodName([ParameterList])\,[throws\,Exception]\{\dots\}
  • Types of Methods:
    • Instance Method: Defined without the staticstatic keyword.
    • Static Method: Defined with the staticstatic keyword. The mainmain 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();ClassName\,obj = new\,ClassName();).
    • Instance Area: Can access both instance and static members directly without creating an object.
  • Object Creation Syntax: ClassNameobjName=newClassName();ClassName\,objName = new\,ClassName();
  • 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 IDID is set to 1010 in the class, and then modified to 2020 using obj1.IDobj1.ID, a newly created obj2.IDobj2.ID will still show the original value of 1010. 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.