Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to get 'http' String literal in Java?

Tags:

java

To create an URL e.g. by

URL url = new URL("http", "localhost", "test.txt");

I have to use the constant String "http". Also in many other code lines. I was not able to find any constant that contains this string (or even better a constant collection of all common protocols).

Are there any existing constants for protocols?

like image 452
gorootde Avatar asked Jun 16 '16 06:06

gorootde


People also ask

Does Java have string literal?

A string literal in Java is basically a sequence of characters from the source character set used by Java programmers to populate string objects or to display text to a user.

Can I use Backticks in Java?

Unlike Kotlin, Groovy or Python, which use """ Java decided to use backtick ` notation, which is currently used by Javascript or Go. No matter how many backticks you use or whether you use raw or original string literals, the resulting .

How do you close a string literal in Java?

Unescaped double quotes inside string literal In the case of the double quote ( " ), it has to be escaped with a preceding backslash ( \ ) so that it doesn't get misinterpreted as the character marking the end of the string.


2 Answers

Short Answer :

There is no final string literal http constant in the Java SE JDK at the time of this post.

Longer Answer

I realize this is an old question but I don't think it has necessarily been answered adequately. I believe that Leon's answer is partially correct. Having a constant named HTTP with string value "http" is pointless in many cases. However there are situations where this is not pointless. Additionally this question illustrates the pervasive misunderstanding of what a protocol is and is not.

Constant, Protocol, Scheme

There are three main reasons to use final string literals in Java.

  • If you are going to use the same value in multiple places and want a single place to change the value if needed in the future.
  • To avoid the use of magic numbers or their String counterparts.

The first point is self evident but not likely in this case. The second point is applicable to the constant "http", but it depends on the usage, which I will briefly touch upon below as it relates to this example.

HTTP is an application protocol used for the transfer of hypermedia. It is one of many protocols defining how data is transferred over the world wide web (a term that is often confused with "internet"). The most recognizable aspect of the HTTP application protocol is the http: you see before a URI. However, that http: you see is not the protocol, it is the scheme. The scheme is a piece of the protocol, but is not the protocol. In that case it might be apt to have the following string literal constant.

final String SCHEME = "http";

Additionally, if you were writing some type of service client that works over multiple protocols, you might want to specify a default one. In that case the following final string literal would be appropriate.

final String DEFAULT_SCHEME = "http";

Does one exist?

Indeed the following text is supported by the fact that Apache does have an http constant that is used for the reason mentioned above.

org.apache.http.HttpHost.DEFAULT_SCHEME_NAME

This constant is also included in the older httpclient library when it was part of Apache Commons (it is now part of HttpComponent).

Recommendation

As of Java 9 there exists no Java SE constant for http. I would favor creating your own constant instead of using Apache's. I recommend this is because Apache's DEFAULT_SCHEME_NAME constant is specifically referring to the HttpHost class. Utilizing it for something else violates the single purpose principal of software engineering. To be more pragmatic, the HttpHost class could change their default scheme (maybe) without you changing your default scheme. This would obviously be an issue. However, Java EE does provide you with final string literals for all the HTTP methods, response statuses, header fields, and a myriad of other HTTP-related things such as authentication types, etc. I relate with your desire to have these string literals provided as constants in Javas standard library. It would just make me feel more whole.

Post Script

I want to point out another usage of a final string literals that was made by user @gorootde in a comment below.

The example

final String Z = "Z";

may still have value if this result is used multiple times throughout the code base in contrast to other "Z" that are of different significance. Without knowing the context I can't judge but this would be an odd choice that would require significant justification, however it's not outright pointless by definition.

like image 147
Chris Maggiulli Avatar answered Oct 30 '22 09:10

Chris Maggiulli


There is this constant field http in the Apache library:

org.apache.http.HttpHost.DEFAULT_SCHEME_NAME

If you are using this library in your application, you can use it in your case.

like image 39
krit273 Avatar answered Oct 30 '22 10:10

krit273