Latest / CyberSecurity Summary / Core Java SE 9 for the Impatient
Transcript
- 0:00Speaker 1: If you ask a modern supercomputer today to calculate two
- 0:03Speaker 1: point zero minus one point one, it actually gets the
- 0:07Speaker 1: answer wrong.
- 0:08Speaker 2: Yeah, it completely fails.
- 0:09Speaker 1: Right, it won't give you zero point nine, It'll spit
- 0:11Speaker 1: out zero point eight nine, nine nine, just stretching out
- 0:15Speaker 1: until it finally gives up, which is wild, right it is,
- 0:18Speaker 1: And welcome to the deep dive. Today, we're exploring why
- 0:21Speaker 1: the most powerful machines on are still struggle with you know,
- 0:24Speaker 1: grade school arithmetic.
- 0:25Speaker 2: And more importantly, how we navigate those limits as developers.
- 0:29Speaker 1: Exactly, So for you the listener, we are taking on
- 0:31Speaker 1: a highly targeted mission.
- 0:33Speaker 2: Today.
- 0:33Speaker 1: We are unpacking excerpts from Core Jama Se nine for
- 0:37Speaker 1: the Impatient, second Edition by K. S.
- 0:40Speaker 2: Horseman, which is such an essential read because I mean,
- 0:43Speaker 2: Java is over two decades old.
- 0:45Speaker 1: Yeah, it's ancient in tech years, right.
- 0:47Speaker 2: The ecosystem is just burdened by these layers of legacy architecture.
- 0:51Speaker 2: If you pick up a traditional textbook, you're looking at
- 0:54Speaker 2: thousands of pages dwelling on.
- 0:56Speaker 1: Ancient history, making you trace the lineage of archaic libe
- 1:00Speaker 1: before you can even write a basic concurrent application.
- 1:04Speaker 2: Exactly. It's exhausting.
- 1:05Speaker 1: So our mission today is to cut through all of
- 1:07Speaker 1: that noise. We're extracting the modern practical tools introduced in
- 1:11Speaker 1: Java nine, so you can skip the computer science theory
- 1:14Speaker 1: of the nineteen nineties.
- 1:15Speaker 2: It's straight to building scalable server side systems.
- 1:19Speaker 1: But you know, to appreciate the modern workarounds Java nine
- 1:22Speaker 1: gives us, we first have to establish why we're still
- 1:25Speaker 1: building on top of this twenty year old foundation at all.
- 1:28Speaker 2: Right, because in the fast paced tech world, twenty years
- 1:31Speaker 2: is basically the Mesozoic era.
- 1:34Speaker 1: Seriously, yet Java is still everywhere. Why is that?
- 1:37Speaker 2: Well, the survival of the language really comes down to
- 1:39Speaker 2: its initial architectural promise, which is the Java Virtual Machine
- 1:43Speaker 2: the JVM.
- 1:44Speaker 1: Okay, let's unpack this.
- 1:45Speaker 2: Prior to this paradigm, if a developer wrote a program,
- 1:48Speaker 2: that code had to be compiled specifically for the underlying hardware.
- 1:52Speaker 1: Architecture, like physically tied to the machine.
- 1:55Speaker 2: Yes, you needed a distinct compilation for a Windows environment,
- 1:59Speaker 2: another for a Mac and yet another for a Linux server.
- 2:02Speaker 1: Which sounds like a nightmare it.
- 2:04Speaker 2: Was, But Java decoupled the software from the physical hardware.
- 2:09Speaker 2: You compile your source code once into an intermediate state
- 2:12Speaker 2: called bytecode.
- 2:13Speaker 1: A universal instruction set basically exactly which.
- 2:16Speaker 2: The JVM then takes over. As long as the device
- 2:19Speaker 2: has a JVM installed, it can interpret that bytecode on.
- 2:22Speaker 1: The fly, so it translates the intermediate instructions into the
- 2:26Speaker 1: specific machine code of the host device.
- 2:28Speaker 2: Right, whether that's a high frequency trading server or I
- 2:31Speaker 2: don't know, a smart refrigerator. Oh wow. Yeah, that decoupling
- 2:35Speaker 2: is why massive enterprise back ends and Android ecosystems are
- 2:39Speaker 2: entirely reliant on it today.
- 2:41Speaker 1: Right, once run anywhere. It was a brilliant design, it
- 2:44Speaker 1: really was. But you know, to achieve that massive enterprise
- 2:47Speaker 1: level scale, Java developed a reputation for being heavily bureaucratic.
- 2:51Speaker 1: It's incredibly verbose.
- 2:53Speaker 2: Oh. Absolutely.
- 2:53Speaker 1: To appreciate the first massive shift in Java nine, we
- 2:57Speaker 1: have to look at the classic Hello.
- 2:58Speaker 2: World program rite a passage.
- 3:00Speaker 1: Right in almost any other scripting language, if you want
- 3:03Speaker 1: to output a string of text to the console, you
- 3:06Speaker 1: invoke a print command and you're done.
- 3:08Speaker 2: But traditional Java demands a rigid structural hierarchy before it
- 3:13Speaker 2: will execute a single command.
- 3:14Speaker 1: Yeah, you have to wrap everything. In an object oriented paradigm,
- 3:18Speaker 1: you declare a public class, allocate a name space inside.
- 3:21Speaker 2: That class, you declare a public static void main.
- 3:24Speaker 1: Method cassing it and array of string arguments. Only then
- 3:27Speaker 1: can you call system dot out, dot printle in.
- 3:30Speaker 2: It's a lot of overhead.
- 3:31Speaker 1: It's like imagine wanting to fry a single egg for breakfast,
- 3:35Speaker 1: but the environment insists that before you turn on the stove,
- 3:37Speaker 1: you must zone a commercial plot, build a fully permitted
- 3:41Speaker 1: stainless steel kitchen, and pass a municipal health inspection.
- 3:46Speaker 2: That is Yeah, that's a perfect analogy. It's a phenomenal
- 3:48Speaker 2: structure if you're running a massive restaurant franchise, but it's
- 3:52Speaker 2: deeply punishing if you just want to test a single
- 3:54Speaker 2: line of code. Right, and that uniformity of forcing everything
- 3:57Speaker 2: into a class hierarchy is what makes million line code
- 4:00Speaker 2: bases comprehensible. Sure, but as you pointed out, the friction
- 4:03Speaker 2: for experimentation is incredibly high. So Java nine addresses this
- 4:08Speaker 2: directly by introducing jshell.
- 4:11Speaker 1: The jshell revolution exactly.
- 4:13Speaker 2: It's an r epl or reevaluate print loop. You open
- 4:16Speaker 2: a terminal type jshell and you're dropped into a live,
- 4:19Speaker 2: interactive environment.
- 4:20Speaker 1: Okay, I want to look under the hood here, because
- 4:23Speaker 1: JAMA is a strictly compiled language. Right, You can't just
- 4:26Speaker 1: run raw Java code like it's a Python script. If
- 4:29Speaker 1: I'm sitting in jshall and I just type, you know,
- 4:32Speaker 1: quote hello world, unquote dot length. How is the JVM
- 4:36Speaker 1: executing that without me running a compiler first.
- 4:39Speaker 2: Well, what's fascinating here is jshell actually leverages the compiler
- 4:43Speaker 2: api introduced in earlier versions of Java. Okay, when you
- 4:47Speaker 2: type that single snippet and hit enter, jshell secretly takes
- 4:50Speaker 2: your fragment of code and wraps it in a synthetic
- 4:53Speaker 2: invisible class.
- 4:54Speaker 1: Wait really it does that automatically.
- 4:56Speaker 2: Yep. It runs at synthetic class through the Java compiler
- 4:59Speaker 2: entirely in the system's memory bypassing the hard drive completely.
- 5:02Speaker 1: Oh wow.
- 5:03Speaker 2: It manages the class pack dependencies, dynamically loads the resulting
- 5:07Speaker 2: bytecode into the running JVM, executes it, and prints the
- 5:10Speaker 2: result back to your terminal.
- 5:11Speaker 1: So it is doing all of that boilerplate commercial kitchen
- 5:14Speaker 1: construction invisibly in the background, in milliseconds.
- 5:18Speaker 2: Exactly, giving you the illusion of an interpreted scripting language.
- 5:22Speaker 1: That's credible.
- 5:23Speaker 2: The environment even maps your live session state. If you
- 5:27Speaker 2: declare an integer variable x, jshell keeps that variable alive
- 5:31Speaker 2: in memory for the duration of the seession, allowing you
- 5:34Speaker 2: to manipulate it in subsequent lines.
- 5:36Speaker 1: That fundamentally alters the development workflow totally.
- 5:39Speaker 2: Instead of setting up a heavy ide and fighting with
- 5:41Speaker 2: build tools just to see how a specific method behaves,
- 5:44Speaker 2: you test the logic instantly.
- 5:46Speaker 1: It encourages a sandbox approach, and once you have this
- 5:50Speaker 1: instant feedback loop in jshell, I feel like the very
- 5:52Speaker 1: first thing a developer usually tests is mathematical logic.
- 5:56Speaker 2: Which brings us back to that supercomputer failing its subtraction.
- 5:59Speaker 1: Yes, when I ask jshell to calculate two point zero
- 6:03Speaker 1: minus one point one, the output is that trailing string
- 6:07Speaker 1: of nines zero point eight nine nine nine nine. Why
- 6:10Speaker 1: doesn't the language just fix this? Why not make everything
- 6:13Speaker 1: infinitely precise automatically. I mean, we aren't dealing with the
- 6:16Speaker 1: simple software bug here.
- 6:18Speaker 2: Right, No, not at all. This is a fundamental limitation
- 6:22Speaker 2: of hardware architecture. We are hitting the constraints of the
- 6:25Speaker 2: IE seven five y four standard for floating point arithmetic.
- 6:29Speaker 1: Okay, break that down for us.
- 6:30Speaker 2: Well. Human beings calculate in base ten, where fractions like
- 6:34Speaker 2: one tenth have a clean definitive representation, but computers operate
- 6:38Speaker 2: in base two binary logic, mapping data into fixed physical
- 6:42Speaker 2: memory blocks.
- 6:43Speaker 1: So a standard double precision floating point number in Java
- 6:47Speaker 1: a double is allocated exactly sixty four bits of physical
- 6:51Speaker 1: memory space.
- 6:52Speaker 2: Yes, the CPU divides those sixty four bits into a
- 6:54Speaker 2: specific structure one bit for the sign, eleven bits for
- 6:58Speaker 2: the exponent, and fifty two bits for the main time
- 7:00Speaker 2: or the significant digits. Got it. When you ask the
- 7:02Speaker 2: CPU to represent the base ten fraction of one tenth
- 7:05Speaker 2: in binary, it becomes a repeating infinite sequence of zeros
- 7:08Speaker 2: and ones.
- 7:09Speaker 1: Similar to trying to write out one third as a decimal.
- 7:12Speaker 1: You get zero point three three three, and eventually you
- 7:14Speaker 1: just run out of paper and have to stop writing.
- 7:16Speaker 2: Exactly The sixty four bit memory block is the edge
- 7:18Speaker 2: of the paper. The CPU literally runs out of physical
- 7:22Speaker 2: transistors to hold the infinite binary sequence, so it truncates it.
- 7:26Speaker 2: It chops off the end, which introduces a microscopic roundoff.
- 7:30Speaker 1: Error, and when the system converts that truncated binary value
- 7:33Speaker 1: back into base ten for your terminal screen, the missing
- 7:37Speaker 1: precision reveals itself as that endless string of nine or feislely. Now,
- 7:41Speaker 1: if you are building a physics engine for a video game,
- 7:43Speaker 1: a microscopic roundoff error doesn't matter.
- 7:46Speaker 2: No nobody notices.
- 7:47Speaker 1: But if I'm writing a financial ledger application, I cannot
- 7:49Speaker 1: lose fractions of ascent on every transaction. I need exact precision.
- 7:54Speaker 2: Right, which is illegal in some context.
- 7:56Speaker 1: Honestly, I assume this means Java forces developers to choose
- 7:59Speaker 1: between hardware speed and mathematical accuracy.
- 8:02Speaker 2: That is, the exact tradeoff. Primitive types like double end
- 8:05Speaker 2: float are blazingly fast because they map directly to the
- 8:08Speaker 2: CPU's native instruction set.
- 8:10Speaker 1: They execute in a single clock cycle.
- 8:12Speaker 2: Right. But if you require absolute precision for financial math,
- 8:16Speaker 2: the text explicitly warns against primitive types, you must invoke
- 8:20Speaker 2: the big decimal class.
- 8:22Speaker 1: Which bypasses the CPU's native floating point logic entirely exactly.
- 8:27Speaker 2: Big decimal is an object. It implements arbitrary precision arithmetic
- 8:31Speaker 2: and software.
- 8:31Speaker 1: Oh so it handles the math itself.
- 8:33Speaker 2: Yeah, It dynamically allocates as much heat memory as necessary
- 8:37Speaker 2: to store the exact decimal representation of the number it
- 8:41Speaker 2: guarantees zero point nine.
- 8:42Speaker 1: But the cost is performance because every mathematical operation requires
- 8:47Speaker 1: object instantiation, method dispatch, and eventual garbage collection.
- 8:51Speaker 2: Yes, it's an active architectural choice the developer has to make.
- 8:54Speaker 1: And this hardware limitation applies to whole numbers as well.
- 8:57Speaker 2: Right, oh, absolutely.
- 8:58Speaker 1: The text highlights a terrified trap with standard integers. If
- 9:02Speaker 1: you take a standard integer variable and assign up the
- 9:04Speaker 1: value of one billion, and then multiply it by three,
- 9:07Speaker 1: you don't get three billion.
- 9:08Speaker 2: No, you don't.
- 9:09Speaker 1: Java quietly outputs negative one billion, two hundred and ninety
- 9:13Speaker 1: four million, nine hundred and sixty seven thousan two hundred
- 9:16Speaker 1: and ninety six. The logic just violently inverts.
- 9:18Speaker 2: It's crazy. So a standard in in Java is allocated
- 9:21Speaker 2: thirty two bits of memory space and a two's complement
- 9:24Speaker 2: binary representation, which is how modern processors handle signed integers.
- 9:28Speaker 2: The maximum positive value you can fit into those thirty
- 9:31Speaker 2: two bits is just over two billion.
- 9:33Speaker 1: Because the leftmost bit, the thirty second bit, is reserved
- 9:36Speaker 1: as the sign bit.
- 9:37Speaker 2: Right. If it's a zero, the number is positive. If
- 9:40Speaker 2: it's a one, the CPU interprets the entire sequence as
- 9:43Speaker 2: a negative number.
- 9:45Speaker 1: So when you multiply one billion by three, the resulting
- 9:47Speaker 1: binary sequence is too large for the thirty one bits
- 9:50Speaker 1: allocated for the magnitude exactly.
- 9:53Speaker 2: The binary logic overflows, spilling a one into that thirty
- 9:57Speaker 2: second sign big position. Ah. I see the CPU you
- 10:00Speaker 2: blindly follows its architecture. It sees a one in the
- 10:03Speaker 2: sign position and interprets the resulting garbled sequence as a
- 10:07Speaker 2: massive negative number.
- 10:08Speaker 1: What makes this uniquely dangerous and traditional Java is that
- 10:12Speaker 1: the multiplication operator the standard asterisk, does this silently.
- 10:15Speaker 2: That's the worst part. It doesn't warn you.
- 10:17Speaker 1: The binary spills over the sign flips, and your e
- 10:20Speaker 1: commerce application suddenly thinks a customer ordering three billion dollars
- 10:24Speaker 1: worth of inventory actually has a negative balance.
- 10:26Speaker 2: Effectively owning the money. It is a silent data corruption.
- 10:30Speaker 1: So what's the modern Java solution for this? Well?
- 10:32Speaker 2: Java nine offers a targeted solution in the math library.
- 10:36Speaker 2: Instead of relying on the native multiplication operator, you invoke
- 10:39Speaker 2: math dot multiplay exact.
- 10:41Speaker 1: Oh okay. Does it allocate more memory to hold the
- 10:44Speaker 1: larger number.
- 10:45Speaker 2: No, it retains the thirty two bit limitation, but it
- 10:48Speaker 2: actively monitors the operation. Math dot multiply exact bridges the
- 10:53Speaker 2: high level language and the low level CPU logic a soo.
- 10:56Speaker 2: It either checks the hardware's internal overflow flag or simulates
- 11:00Speaker 2: the boundary check. If the calculation exceeds the physical memory limit,
- 11:04Speaker 2: it intercepts the operation and throws an arithmetic.
- 11:07Speaker 1: Exception, so it crashes the program on purpose. Exactly, it
- 11:11Speaker 1: forces a loud failure rather than permitting a silent corruption,
- 11:14Speaker 1: which is much safer. So primitive math is strictly bound
- 11:17Speaker 1: by the physical architecture of the CPU registers. But if
- 11:21Speaker 1: you think numbers are fragile in memory, the way Java
- 11:23Speaker 1: handles human language is a completely different level of complexity.
- 11:27Speaker 2: Oh, mathematical logic is universal human language, and the character
- 11:31Speaker 2: encodings required to render it are cultural artifacts mapped onto
- 11:34Speaker 2: physical memory.
- 11:35Speaker 1: That's a great way to put it. The text introduces
- 11:38Speaker 1: the string class and drops a massive warning right away
- 11:42Speaker 1: regarding conditional logic. It says never use the double equals
- 11:46Speaker 1: operator to compare two strings.
- 11:48Speaker 2: It causes bugs that can take days to track down
- 11:52Speaker 2: because occasionally using double equals actually works.
- 11:56Speaker 1: Which creates a false sense of security. Exactly, pack the
- 12:00Speaker 1: pointer mechanics here, because this is where the logic breaks
- 12:02Speaker 1: down for developers coming from languages like Python. The double
- 12:06Speaker 1: equals operator in Java doesn't care about the text itself.
- 12:10Speaker 1: It is evaluating the hexadecimal memory address.
- 12:13Speaker 2: It's asking do these two variables point to the exact
- 12:16Speaker 2: same physical location in the heap.
- 12:18Speaker 1: Whereas the dot equals method forces the JVM to travel
- 12:22Speaker 1: to those memory addresses and iterate through the underlying character arrays,
- 12:26Speaker 1: comparing them bite by bite to see if the contents match.
- 12:29Speaker 2: Right, But you mentioned it sometimes works. Yeah.
- 12:31Speaker 1: If I define a string ex's hello and why is
- 12:33Speaker 1: hello and use double equals, Java might actually return true.
- 12:37Speaker 1: Why would it do that if there are separate variables?
- 12:39Speaker 2: So the JVM implements a memory optimization technique called string
- 12:43Speaker 2: in turning. Okay, Because strings are immutable in Java, meaning
- 12:46Speaker 2: they cannot be changed what's created, The JVM maintains a
- 12:51Speaker 2: string pool in the.
- 12:52Speaker 1: Heap, a pool of existing strings.
- 12:54Speaker 2: Right, if you declare a string literal hello, the JVM
- 12:58Speaker 2: checks the pool. If Hello already exists. It doesn't allocate
- 13:02Speaker 2: new memory. It simply points your new variable to the
- 13:05Speaker 2: existing memory address.
- 13:08Speaker 1: So, in that specific scenario, double equals returns true because
- 13:11Speaker 1: both variables point to the identical address exactly.
- 13:14Speaker 2: But the second you manipulate the text, say you extract
- 13:17Speaker 2: a substring or dynamically a panda character, the JVM is
- 13:21Speaker 2: forced to allocate a completely new memory address for the result, and.
- 13:24Speaker 1: At that point double equals returns false, even if the
- 13:27Speaker 1: resulting text looks identical on the screen.
- 13:29Speaker 2: Yep, it's comparing two different hecks addresses. You are bypassing
- 13:32Speaker 2: the intern pool. If you don't understand the pointer mechanics,
- 13:35Speaker 2: the behavior appears entirely erratic.
- 13:38Speaker 1: Here's where it gets really interesting. The illusion of text
- 13:41Speaker 1: goes even deeper when we examine the chart type.
- 13:43Speaker 2: Oh, this is fascinating.
- 13:44Speaker 1: This forces us to look at the historical context of
- 13:47Speaker 1: Java's architecture. Java was designed in nineteen ninety one, during
- 13:51Speaker 1: a period of complete chaos in character encoding.
- 13:54Speaker 2: Total chaos. You had to ask you for English, which
- 13:56Speaker 2: used seven bits, but Western European languages used different and
- 14:00Speaker 2: eight bit extensions. Russian had KOI eight Japanese had shift jis.
- 14:04Speaker 1: None of them were compatible.
- 14:06Speaker 2: No, a text file created in Tokyo looked like pure
- 14:09Speaker 2: gibberish on a terminal in London.
- 14:11Speaker 1: So the industry solution was the Unicode standard, and in
- 14:14Speaker 1: nineteen ninety one the architects of Unicode decided that a
- 14:17Speaker 1: sixteen bit standard would be sufficient.
- 14:19Speaker 2: Right, sixteen bits gives you exactly sixty five, five hundred
- 14:22Speaker 2: and thirty six available character slots.
- 14:24Speaker 1: At the time that seemed infinitely large. They assumed it
- 14:27Speaker 1: could comfortably hold every alphabet and symbol.
- 14:29Speaker 2: On Earth, and Java was built from the ground up
- 14:32Speaker 2: to natively support this new sixteen bit Unicode standard. The
- 14:36Speaker 2: foundational char primitive type in Java was permanently defined as
- 14:39Speaker 2: a sixteen bit block of.
- 14:40Speaker 1: Memory, which became a catastrophic legacy trap because sixty five
- 14:44Speaker 1: thousand slots was nowhere.
- 14:45Speaker 2: Near enough, not even close. By the time they incorporated
- 14:48Speaker 2: comprehensive Chinese ideographs, obscure historical scripts, and complex mathematical formatting,
- 14:55Speaker 2: the Unicode Consortium blew right past the sixty five thousand
- 14:59Speaker 2: limit they ran. Yeah, they were forced to expand Unicode
- 15:02Speaker 2: to a twenty one bit standard, which supports over a
- 15:05Speaker 2: million characters.
- 15:06Speaker 1: But Java's architecture was already set in stone. A Java
- 15:10Speaker 1: string is essentially an array of these sixteen bit char
- 15:13Speaker 1: blocks exactly. So how does Java handle a twenty one
- 15:16Speaker 1: bit character if the physical memory bucket is only sixteen
- 15:19Speaker 1: bits wide.
- 15:19Speaker 2: Well, it employs a patchwork solution called surrogate pairs.
- 15:22Speaker 1: Surrogate pair.
- 15:23Speaker 2: Yeah, it takes the twenty one bit character and slices
- 15:26Speaker 2: it into two distinct sixteen bit code units, a high
- 15:30Speaker 2: surrogate and a low serrogu.
- 15:32Speaker 1: The text uses the example of the mathematical symbol for
- 15:35Speaker 1: the set of Octonians. It's a single character on the screen,
- 15:38Speaker 1: but underneath it spans two sixteen bit memory blocks. Right,
- 15:41Speaker 1: I can see exactly where this breaks are application logic.
- 15:44Speaker 1: If I use the standard dot length method on a
- 15:46Speaker 1: string containing the Octonian symbol, it isn't counting the characters
- 15:49Speaker 1: I see on the screen. Yeah, it's counting the sixteen
- 15:52Speaker 1: bit memory blocks exactly.
- 15:54Speaker 2: The dot length method will return a value of two.
- 15:56Speaker 1: And if I try to iterate through that string using
- 15:59Speaker 1: a standard loops so grabbing characters one by one to
- 16:02Speaker 1: parse them, my logic will rip that surrogate pair apart.
- 16:05Speaker 2: It completely breaks it.
- 16:06Speaker 1: I'll pull the high surrogate on the first pass, which
- 16:09Speaker 1: is a meaningless piece of data on its own, completely
- 16:12Speaker 1: corrupting the data pipeline.
- 16:14Speaker 2: If we connect this to the bigger picture, to process
- 16:17Speaker 2: modern texts safely, Java nine forces you to abandon the
- 16:21Speaker 2: concept of characters.
- 16:22Speaker 1: Okay, so what do we use instead?
- 16:23Speaker 2: You have to process text using code points. A code
- 16:27Speaker 2: point represents the true valid twenty one bit Unicode value.
- 16:31Speaker 2: Got it, You invoke methods like code point at or
- 16:34Speaker 2: code points which return an in stream. These methods are
- 16:37Speaker 2: designed to recognize surrogate pairs in the underlying array, combine
- 16:41Speaker 2: them back into their valid twenty one bit representation, and
- 16:44Speaker 2: hand them to you safely.
- 16:45Speaker 1: It acknowledges the reality that human language no longer fits
- 16:48Speaker 1: neatly into sixteen bit architecture. So we've mapped out how
- 16:52Speaker 1: fragile text is inside the heat memory. But the final
- 16:56Speaker 1: layer we need to look at is how we bring
- 16:58Speaker 1: that text into the application form the user. We need
- 17:01Speaker 1: to look at safe input output.
- 17:02Speaker 2: The front door of your app the interface between the
- 17:05Speaker 2: user's terminal and the system's memory.
- 17:07Speaker 1: Right, the text highlights the scanner class, which is tied
- 17:10Speaker 1: to system dot N. It's the standard tool for reading
- 17:13Speaker 1: console input. You want the user's name, you call next line.
- 17:17Speaker 1: It feels incredibly intuitive, it does. Yet, if stanner is
- 17:20Speaker 1: so great, why does the author explicitly warn us not
- 17:24Speaker 1: to use it for passwords?
- 17:25Speaker 2: Well, it introduces two critical vulnerabilities, one at the interface
- 17:28Speaker 2: level and one deep within the JVM's memory management.
- 17:32Speaker 1: Okay, what's the interface vulnerability.
- 17:34Speaker 2: It's simple, scanner echoes the keystrokes directly back to the terminal.
- 17:37Speaker 2: Anyone looking at the monitor over your shoulder sees the
- 17:40Speaker 2: password in plain text.
- 17:41Speaker 1: That's the obvious flaw, right.
- 17:43Speaker 2: But the deeper vulnerability relates to what scanner actually returns
- 17:47Speaker 2: to your code. Yeah, it returns a string object.
- 17:49Speaker 1: And we just established that strings and Java are immutable.
- 17:52Speaker 2: Exactly once you accept that password and store it in
- 17:57Speaker 2: a string variable, it occupies a specific physical location in
- 18:01Speaker 2: the heap. Because it is immutable, you cannot wipe it.
- 18:05Speaker 1: Wait, really, you can't just delete it.
- 18:07Speaker 2: No, If you assign a new value to that variable,
- 18:10Speaker 2: you are simply pointing the variable to a different memory address.
- 18:14Speaker 2: The original password remains sitting in the heap as an
- 18:16Speaker 2: orphaned object.
- 18:17Speaker 1: It's just floating there waiting for the jo garbage collector
- 18:20Speaker 1: to notice it's no longer being used and sweep it away.
- 18:23Speaker 2: Yeah, but garbage collection in Java is nondeterministic. It doesn't
- 18:27Speaker 2: run instantly. It runs when the heap starts getting full
- 18:31Speaker 2: or when the CPU has idle.
- 18:32Speaker 1: Cycles, So it could be sitting there for a while.
- 18:35Speaker 2: Depending on the garbage collection algorithm. Your JVM is running,
- 18:38Speaker 2: an orphan string could sit in memory for milliseconds, or
- 18:41Speaker 2: it could be promoted to an old generation memory space
- 18:44Speaker 2: and sit there for days wow. During that indeterminate window,
- 18:48Speaker 2: if a hacker executes a memory dump of the JVM,
- 18:51Speaker 2: your user's password is sitting right there in plain text.
- 18:55Speaker 1: You completely lose control over the life cycle of the
- 18:57Speaker 1: sensitive data. So for secure authentication, the text points to
- 19:01Speaker 1: the console class. Instead, you invoke system dot console dot
- 19:05Speaker 1: read password. Aside from suppressing the terminal echo, the architectural
- 19:09Speaker 1: brilliance here is that read password doesn't return a string object.
- 19:13Speaker 1: It returns a primitive char array.
- 19:16Speaker 2: And a raisin Java are mutable.
- 19:18Speaker 1: Which means I don't have to wait for the garbage
- 19:20Speaker 1: collector the exact millisecond my authentication logic verifies the password.
- 19:25Speaker 1: I can reach directly into that array's physical memory addresses
- 19:28Speaker 1: and overrate the characters.
- 19:29Speaker 2: With zeros, you actively sanitize the memory block. You destroy
- 19:34Speaker 2: the cryptographic material before the reference is ever dropped, ensuring
- 19:37Speaker 2: that a subsequent memory dump yields nothing but zeros.
- 19:40Speaker 1: You are dictating the exact physical state of the hardware exactly.
- 19:44Speaker 1: So what does this all mean synthesizing all of this.
- 19:47Speaker 1: We started out looking to bypass twenty years of legacy
- 19:50Speaker 1: architecture to find the modern, practical parts of Java. Nine.
- 19:54Speaker 2: Yeah.
- 19:54Speaker 1: The theme here is that being an effective enterprise developer
- 19:57Speaker 1: isn't about memorizing the deep history of nine teen nineties
- 20:00Speaker 1: object oriented theory.
- 20:02Speaker 2: No, It's about understanding the boundaries of the machine and
- 20:06Speaker 2: utilizing the modern layer designed to navigate those boundaries.
- 20:10Speaker 1: It's using jshell to bypass compilation friction and test logic
- 20:14Speaker 1: in an instant. It's invoking big decimal when the CPU's
- 20:17Speaker 1: mantisa limits lie to you. It's utilizing math dot multiply
- 20:21Speaker 1: exec to trap a spilling sign bit before it corrupts
- 20:24Speaker 1: your database. It's parsing strings via code point at because
- 20:28Speaker 1: a sixteen bit array cannot hold a twenty one bit language.
- 20:32Speaker 2: And it is using primitive arrays for cryptography so you
- 20:35Speaker 2: can manually scrub the heat memory before the garbage collector arrives.
- 20:38Speaker 1: It requires working with the grain of the modern APIs
- 20:42Speaker 1: while maintaining a strict awareness of the physical limitations governing
- 20:46Speaker 1: the hardware beneath them.
- 20:47Speaker 2: It's a phenomenal balance of high level abstraction and low
- 20:49Speaker 2: level control.
- 20:50Speaker 1: It really is. But to close this out, there is
- 20:52Speaker 1: a broader implication. Here we discussed how javas string architecture,
- 20:56Speaker 1: the reliance on surrogate pairs, and the breakdown of basic
- 20:59Speaker 1: length checks exist entirely because the architects in nineteen ninety
- 21:02Speaker 1: one assume sixty five thousand characters would be enough for
- 21:05Speaker 1: all of human history.
- 21:06Speaker 2: To them, sixteen bits represented an infinite horizon. They couldn't
- 21:11Speaker 2: conceptualize a use case that would exceed.
- 21:13Speaker 1: It, which forces us to look at what we are
- 21:16Speaker 1: building today as developers construct the foundational architecture for distributed AI,
- 21:21Speaker 1: neural networks and quantum state management. What current infinite limit
- 21:26Speaker 1: are we hard coding into our systems?
- 21:28Speaker 2: That's the real question.
- 21:29Speaker 1: What arbitrary data cap or memory structure are we setting
- 21:32Speaker 1: in stone right now that will become a massive structural
- 21:35Speaker 1: trap for engineers twenty years down the line.
- 21:38Speaker 2: Because the architectures we assume are boundless today will inevitably
- 21:42Speaker 2: become the legacy bottlenecks of tomorrow.
- 21:44Speaker 1: We are undoubtedly laying our own sixteen bit traps without
- 21:47Speaker 1: even realizing it.
- 21:48Speaker 2: Very true.
- 21:49Speaker 1: Thank you for joining us on this deep dive. Keep
- 21:51Speaker 1: questioning the abstractions, look under the hood of your tools,
- 21:54Speaker 1: and we'll see you next time.