Day 1 - Start with the Fundamentals

Day 1 -  Start with the Fundamentals

  • Introduction to Java & Its Features
  • Java Virtual Machine (JVM)
  • Setting Up the JAVA Environment
  • Hello World Program in JAVA
  • Integrated Development Environments (IDEs) for JAVA

Java Programming Language

Java is one of the most popular and widely used programming languages.

  • Java has been one of the most popular programming languages for many years.
  • Java is Object Oriented. However, it is not considered as pure object-oriented as it provides support for primitive data types (like int, char, etc)
  • The Java codes are first compiled into byte code (machine-independent code). Then the byte code runs on Java Virtual Machine (JVM) regardless of the underlying architecture.
  • Java syntax is similar to C/C++. But Java does not provide low-level programming functionalities like pointers. Also, Java codes are always written in the form of classes and objects.
  • Java is used in all kinds of applications like Mobile Applications (Android is Java-based), desktop applications, web applications, client-server applications, enterprise applications, and many more.
  • When compared with C++, Java codes are generally more maintainable because Java does not allow many things which may lead to bad/inefficient programming if used incorrectly. For example, non-primitives are always references in Java. So we cannot pass large objects (like we can do in C++) to functions, we always pass references in Java. One more example, since there are no pointers, bad memory access is also not possible.
  • When compared with Python, Java kind of fits between C++ and Python. The programs are written in Java typically run faster than corresponding Python programs and slower than C++. Like C++, Java does static type checking, but Python does not.

 

Java programming language is named JAVA. Why?

After the name OAK, the team decided to give a new name to it and the suggested words were Silk, Jolt, revolutionary, DNA, dynamic, etc. These all names were easy to spell and fun to say, but they all wanted the name to reflect the essence of technology. In accordance with James Gosling, Java the among the top names along with Silk, and since java was a unique name so most of them preferred it.

Java is the name of an island in Indonesia where the first coffee(named java coffee) was produced. And this name was chosen by James Gosling while having coffee near his office. Note that Java is just a name, not an acronym.

Java Terminology

Before learning Java, one must be familiar with these common terms of Java.

1.  Java Virtual Machine(JVM):  This is generally referred to as JVM. There are three execution phases of a program. They are written, compile and run the program.

  • Writing a program is done by a java programmer like you and me.
  • The compilation is done by the JAVAC compiler which is a primary Java compiler included in the Java development kit (JDK). It takes the Java program as input and generates bytecode as output.
  • In the Running phase of a program, JVM executes the bytecode generated by the compiler.

Now, we understood that the function of Java Virtual Machine is to execute the bytecode produced by the compiler. Every Operating System has a different JVM but the output they produce after the execution of bytecode is the same across all the operating systems. This is why Java is known as a platform-independent language.

2. Bytecode in the Development process:  As discussed, the Javac compiler of JDK compiles the java source code into bytecode so that it can be executed by JVM. It is saved as .class file by the compiler. To view the bytecode, a disassembler like javap can be used.

3. Java Development Kit(JDK): While we were using the term JDK when we learn about bytecode and JVM. So, as the name suggests, it is a complete Java development kit that includes everything including compiler, Java Runtime Environment (JRE), java debuggers, java docs, etc. For the program to execute in java, we need to install JDK on our computer in order to create, compile and run the java program.

4. Java Runtime Environment (JRE): JDK includes JRE. JRE installation on our computers allows the java program to run, however, we cannot compile it. JRE includes a browser, JVM, applet supports, and plugins. For running the java program, a computer needs JRE.




String Manipulation Interview questions?

 *************************************************************************************

String concat,replace, substring, trim, lowercase, uppercase? Selenium, Automation core java interview questions?


*************************************************************************************

public class StringManipulation {

    public static void main(String[] args) {
        // Strings are immutable
        String str3 = "java";
        str3.concat("oops");
        System.out.println(str3); // java

        // The result should be assigned to a new reference variable (or same
        // variable) can be reused.
        String concat = str3.concat("value2");
        System.out.println(concat); // value1value2

        // -----------------------String methods-------------------------------------
        String str = "abcdefghijk";

        // char charAt(int paramInt)
        System.out.println(str.charAt(2)); // prints a char - c

        // String concat(String paramString)
        System.out.println(str.concat("lmn"));// abcdefghijklmn

        System.out.println("ABC".equalsIgnoreCase("abc"));// true
        System.out.println("ABCDEFGH".length());// 8

        // String replace(char paramChar1, char paramChar2)
        System.out.println("012301230123".replace('0', '4'));// 412341234123

        // String replace(CharSequence paramCharSequence1, CharSequence
        // paramCharSequence2)
        System.out.println("012301230123".replace("01", "45"));// 452345234523

        // All characters from index paramInt
        // String substring(int paramInt)
        System.out.println("abcdefghij".substring(3)); // defghij
        // 0123456789

        // All characters from index 3 to 6
        System.out.println("abcdefghij".substring(3, 7)); // defg
        // 0123456789

        System.out.println("ABCDEFGHIJ".toLowerCase()); // abcdefghij

        System.out.println("abcdefghij".toUpperCase()); // ABCDEFGHIJ

        System.out.println("abcdefghij".toString()); // abcdefghij

        // trim removes leading and trailings spaces
        System.out.println(" abcd  ".trim()); // abcd
    }
}
 

___________________________________________________________________

What we know about the "STRING"?

 

MCQ on String for concepts and interview preparation QAE - SDET - AUTOMATION QA 2022


1. What is the output for below?

String str1 = "test";
String str2 = "one";

String str3 = str1.concat(str2);


Ans:

a) testone

b) test

c) one

d) Let me check in IDE :)


2. What is the output for below?

String str1 = "test";
String str2 = "test";

String str3 = new String("test");


System.out.println(str1.equals(str2));

System.out.println(str1.equals(str3));


Ans:

a) true,true

b) false, true

c) false, false

d) Let me check in IDE :)


3. What is the output for below?

String str1 = "test";

String str2 = "test";

String str3 = new String("test");


System.out.println(str1==str2));

System.out.println(str1==str3);


a) true, true

b) true, false

c) false, true

d) false, false


4. What is the output for below?

String str = "test";

System.out.println(str1.charAt(2)); 


a) s

b) e

c) error

d) es


5. What is the output for below?

System.out.println("012301230123".replace(55, "45"));

 System.out.println("012301230123".replace("01""45"));


a) compile error, 452345234523

b) 452345234523, 452345234523

c) error, error

d) Let me check in IDE :)


6. What is the output for below?

System.out.println("tes".substring(3));

System.out.println("test".substring(3));



a) error, error

b) error, t

c)  , t

d) t, t


7. How many ways we can create String Object in Java?


a) String s = "abc"; String s1 = new String("abc");

b) Literal, Keyword

c)  Both A and B

d)  Not sure


8. Is String thread safe?


a) true

b) false

c) Not sure


9. What is the output for below?

String str8 = new StringBuilder("test").reverse().toString();

System.out.println(str8);


a) test

b) tset

c) Not sure


10. Which one is thread safe: StringBuffer or StringBuffer ?


a) Both

b) StringBuffer

c) StringBuilder

d) None of the options




Below are the answers for all the questions on STRING:


1. a)testone


2. a)true,true

3. b)true, false

4. a)S

5. a) compile error, 452345234523

6. c)  , t

7. c)  Both A and B

8. a) true

9. b) tset