Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

null? null character?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm very new to java. Hope my question is not boring. What is the difference between null and null character? What is the ASCII value of null literal. (I see null character ASCII value is \u0000 But I want for null literal).
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The null character is \u0000, as you say. However it isn't called that in Java, in fact it doesn't have a special name because there's nothing special about it.

But null? It isn't a character so you can't ask what's its Unicode value. (Not ASCII, please, Java has supported Unicode since its invention 25 years ago.) Instead it's a reference which doesn't refer to anything.

If you're new to Java you may not have heard of references yet. The concept of reference is a central concept in an object-oriented language such as Java. If you haven't heard of references then I'm not going to explain them here, but hopefully you find out soon.
 
Ranch Foreman
Posts: 95
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, when someone speak of such a thing as a "null character" it's often what's called a "zero- (\0) terminated String" which is more a part of languages that do not see a String as a fixed length sequence of characters but rather just a bunch of values in a row terminated by single byte which value is 0x00.
NULL itself has a rather special meaning in programming. In fact, NULL does not have any specified value or representation of any kind - but it is by its own very definition the absence of anything. When one speaks about NULL it does not mean "oh, something has the literal value of 0x00 / \0" but in fact it means that what ever was set to or is NULL just doesn't exists at all.

Try to think about it this way: When you count numbers there's somewhere that special "0" which devides positive numbers from negatives - a rather theoretical construct. But if you have some physical items in front of you - let's say: some cows - and you take away one after the other what you finally end up with isn't really "zero cows" but rather "nothing at all" - so even the information you were counting cows got lost. And that's what you have when one talks about NULL: basically NOTHING at all
 
Saloon Keeper
Posts: 21961
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a side note, all of the ASCII control characters have a 2- or 3-character designation. You undoubtedly recognize CR, LF, and TAB, but the character with code value 0, is known as NUL. That, however, is not a built-in Java name, no more than CR or LF are. If you want to name it, you have to do so yourself.

"null", on the other hand, is a reference to no object. So "null" and NUL are no more alike than apricots and nuclear missiles.

What can make things confusing is that in some languages, such as C, 0 is defined as null and 0 is also directly assignable as a null character value. In fact, the literal 0 is used for: A) null pointer values, B) null character values, C) the integer constant value 0, D) the floating-point constant value 0.0. And probably a D and E that I can't think of offhand. Java, on the other hand, is much more stringent about what you can assign to what.

And incidentally, Many computers start their address spaces at memory location zero. However, not all of them do, and in a lot of cases, memory location zero has some sort of magical significance. But the literal value 0 is first and foremost a null pointer according to the offical C and C++ definitions, so whether or not there is an actual memory location zero is immaterial. That can lead to further confusion, though.
 
thunga buchuka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@TimHolloway , @BobWinter , @PaulClapham Thank you very much for your responses. I actually have no knowledge on any-other programming languages.However, it was confusing when I was going through, Java specification:

https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-3.10.7

If you search for "null", you will see null literal. In Java, I learnt that literal is a value assigned to a variable of certain data type. In specification, it mention "null" as literal for null type which cannot be declared. However, from your explanation, I think null is more like when any value which doesn't have associated memory address and we can null. Am I close?

Another Quick question - I see term "instance" used for String. It says that String is class and methods from it are "instance methods". What exactly is "instance" here? I know dictionary meaning for instance. But exactly is this instance. what is the lifecycle of the variable we create with respect to String class? Sorry if my question is stupid.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thunga buchuka wrote:However, from your explanation, I think null is more like when any value which doesn't have associated memory address and we can null. Am I close?



Yes, that's a good nontechnical way of thinking about it. That's basically my view of it too. But in that page you linked to, after "A null literal is always of the null type" there's a link. You might want to follow that link and read how they expand on that statement. Basically they don't like to say "A null literal is not of any type", probably because it's hard to make that into a logically clear structure, so they introduce the "null type" for the sole purpose of providing a logical explanation.
 
Bob Winter
Ranch Foreman
Posts: 95
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thunga buchuka wrote:What is the ASCII value of null literal.


Ok, in context of the JLS now your question becomes a bit more clear to me.
So, although my reply now depends of your meaning of the term "ASCII value" mine is this: An "ASCII value" first of is only possible for something encoded in ASCII - which is a 7-bit text encoding. So, if you have a text like this post one could work out all the bit patterns for every single charachter in it. But, as NULL is not a text (and hence: no ASCII test) it doesn't have an ASCII value. As already explained: NULL has the special meaning of "absence of anything else". If some reference is set to NULL this means: "This refernce does not 'point' to anything.".
In addition to that: "text" often is refered to "readable" - which limits the range from "full" ASCII 0x00 - 0x7F to only 0x20 - 0x7E as the first 32 codepoints are the basic control characters which don't have a printable representation and 0x7F is the DEL (delete) character - which actually has its origins back on punch cards where when you made a mistake instead of fixing the wrong holes one would had punched out ALL holes which had the special meaning "This character was deleted.". Why DEL made it into ASCII in that way? Because backwards compatibility.
 
thunga buchuka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. That clarifies my doubt on null. Anything on "instance"? Why are methods like length() etc, from String class are called instance methods?
 
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An "instance" is when you have java create memory space to hold an object of a specified class using the "new" operator. You can have zero or more "instances" of a class. An "instance method" requires that you have an instance, aka object, for the method to operate on. This instance, oddly enough, comes into the method as the "this" reference. An "instance" means non-static. Static variables and methods do not require that any instances be created and any static memory and methods are common to all instances that may be created. When discussing methods it is typical to specifically mention when a method is "static", conversely, non-static methods, i.e. "instance methods" typically leave off the word "instance" when discussing them; it is assumed.
 
Tim Holloway
Saloon Keeper
Posts: 21961
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, to put it another way, when you define a Class in Java, that's a template for holding data and/or methods. The Class is an abstraction. To actually create an Object that matches that template, you perform an operation known as "instantiation" (ugh,  ugly word). Instantiation creates an instance of that Class, or, if you prefer, an Object or that class. In Java, the common way to instantiate something is by using operator new, but there's a method named newInstance in Java's java.lang.Class that is basically what operator new invokes.

Java, in common with many other object-oriented languages, is defined in terms of itself, so many of its internal mechanisms such as Class and Object have their own Java implementations.

Incidentally, on IBM's most popular punched-cards, the punches ran in 12 rows, which obviously doesn't lend itself to an 8-bit code such as ASCII. However, paper tape reader/punches were another thing, and that's where you'd see the DEL character's all 8-bit properties make themselves useful. Also, if you botched a punched card, you'd just toss it and punch a new one, but that's harder to do with a 20-foot roll of punched tape. IBM also preferred EBCDIC to ASCII, but that's another story. In really old IBM machines, translation from the 12-bit punched-card readings to EBCDIC was done by software libraries. In later years, the card reader was smart enough to do it itself.

I could give a nice long disseration on the meanings and usage of control characters, but the main concern here I think is more object references and null references, not on the ASCII or Unicode NUL character, which in Java is completely unrelated.
 
thunga buchuka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @Carey Brown, @Tim Holloway. That was helpful. I just progressed my learning into objects topic. I think, I will be having more clarity once I understand Objects in java.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic