JavaRush /Java Blog /Random EN /Coffee break #93. How the String class works in Java. Lea...

Coffee break #93. How the String class works in Java. Learn how to implement a web interface in Java

Published in the Random EN group

How the String class works in Java

Source: DZone I recently came across the following question on a forum: “How many String objects are created here ? One or two?".
String langName = new String("Java");
I was curious how others answered it, knowing that it is a complex question, especially if one is not very knowledgeable about how the String class works in Java. And so, I dove into the comments section. Coffee break #93.  How the String class works in Java.  Learn how to implement a web interface in Java - 1To my surprise, among the commenters there were people who chose “One” as the correct answer, but many more people answered “Two”. For a second, I even began to doubt my knowledge about Strings . The correct answer is that it all depends on the circumstances. The question is not clear enough and leaves room for debate. I would rephrase it like this:

1. How many Java String objects are created in memory when this statement is executed?

There is only one answer.

How many Java String objects will remain in memory after executing this statement?

The answer is two. Hopefully the uncertainty went away once I checked the program's memory dump with a statement like the example above. Coffee break #93.  How the String class works in Java.  Learn how to implement a web interface in Java - 2The program memory dump in the above image shows the existence of two String objects with the same content. This proves that calling the String class constructor and passing a string literal as an argument results in two objects being added to the memory heap: one in regular (non-pool) memory, and the other in the String Constant Pool (SCP), an area in memory that is also part of the heap. Difficulties begin after adding. Calling the constructor always results in a new object being placed in a non-pool area. But the constructor argument, which is a string literal, is also an object, and it is created and stored in SCP during class loading, provided that the string pool does not contain strings with the same content. The Java Language Specification states that “string literals—or, more generally, Strings that are the values ​​of constant expressions (§15.28)—are “interned” to share unique instances using the String method . intern ". Since a string in Java is literal and therefore represents the value of a constant expression, it is pooled. To make this even more obvious, let's rewrite the code presented at the beginning of the article as follows:
String java = "Java";
String langName = new String(java);
Now let's get back to the main question. Will the following statement create one or two String objects :
String langName = new String("Java");
To answer this question and remove any suspicions, let's look at the bytecode of the main method:
public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 11 L0
    NEW java/lang/String
    DUP
    LDC "Java"
    INVOKESPECIAL java/lang/String. (Ljava/lang/String;)V
    ASTORE 1

   L1
    LINENUMBER 14 L1
   FRAME APPEND [java/lang/String]
    GOTO L1

   L2
    LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
    LOCALVARIABLE langName Ljava/lang/String; L1 L2 1
    MAXSTACK = 3
    MAXLOCALS = 2
On line 6 you can see the LDC (Load Constant) command. It loads an element from the string constant pool onto the stack. This means that at the time the constructor is called, the Java literal, which is also an object, has already been added to the pool. This happened while loading a class. Thus, calling the String class constructor with a string literal creates only one object and places it in a non-pooled memory location.

Learn how to implement a web interface in Java

Source: Dev.to The world of JavaScript offers useful and fun web frameworks, there's no doubt about it. There is an interesting alternative for Java developers - the open source Vaadin framework. With it, you can easily implement a graphical user interface (GUI) for the web using only the Java programming language and nothing else. You don't need to write a single line of HTML or JavaScript. A picture is worth a thousand words: Coffee break #93.  How the String class works in Java.  Learn how to implement a web interface in Java - 3

How does Vaadin work?

On the left of the screenshot you see a Java class. This code runs on a server such as Apache Tomcat, Eclipse Jetty or any other servlet container. You can use the API provided by the platform to create user interface (UI) components such as text fields, combo boxes, data grids, date pickers, upload fields, and many others. You can combine these components to create a view (also known as a page or UI), using layouts to arrange user interface (UI) components vertically, horizontally, or any other way using CSS. In the previous example, we created a TextField and a Button by adding them to the VerticalLayout . Once the view is ready, you can open it via a URL using the @Route annotation . In this example, we made it available at http://localhost:8080/hello using @Route("hello") . Vaadin application is a web application in Java. The framework provides a Servlet implementation ( VaadinServlet ) that handles HTTP requests and responses for you. This servlet scans Java classes marked with the @Route annotation to display the correct view in the browser. When a Vaadin application is first requested, Vaadin responds with a lightweight JavaScript file that serves as the client engine. This engine takes care of processing events in the browser and sending them to the server as JSON messages. The VaadinServlet object processes requests and similarly returns JSON responses to the client-side engine. The engine then uses this message to update the elements on the page that need to be updated, if any. Vaadin uses an HTTP session to store a component tree that contains the state of the user interface. Things like components form the representation and their states (enabled/disabled, caption, value, etc.). This all provides a level of security that is worth mentioning. Since the user interface logic is on the server, it is not accessible to attackers. Checks are performed on the server. For example, if a Button is disabled using setEnabled(false) , then this is not just a cosmetic feature in the browser - the server will not run any logic in click listeners added to the disabled button, preventing attackers from exploiting developer tools in browser to change the enabled state or call VaadinServlet to simulate a mouse click (click event).

Is Vaadin free to use?

Yes. Vaadin is an open source framework published under the Apache License 2.0. You don't need to buy anything to create full-fledged web applications with it. There are commercial components that improve performance, but they are not required. For example, there is a visual designer and a CRUD component to help you create views even faster. You can try these components for free before subscribing.

Is Vaadin a replacement for JSP and JSF?

Yes. It can be used as a replacement for JSP, JSF and even JavaScript frameworks like Angular. Many Vaadin customers have successfully migrated from these technologies to Vaadin.

Does Vaadin have support for Spring and Jakarta EE?

Yes. Vaadin has official integration with Spring and Jakarta EE (formerly Java EE). You can add Vaadin as a dependency when creating a new Spring Boot project using Spring Initializr. For Jakarta EE, you can download a simple Hello, World example that uses Jakarta EE. At the time of writing this article, only Jakarta EE version 8 is supported.

Who uses Vaadin?

Many Fortune 500 companies use Vaadin, as well as successful startups and over 200 thousand developers worldwide. Check out the Quick Start Guide and other Vaadin documentation .
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION