{"id":510038,"date":"2024-09-23T07:49:46","date_gmt":"2024-09-23T06:49:46","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=idea&#038;p=510038"},"modified":"2025-08-06T15:51:37","modified_gmt":"2025-08-06T14:51:37","slug":"introduction-to-code-analysis-in-intellij-idea","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/zh-hans\/idea\/2024\/09\/introduction-to-code-analysis-in-intellij-idea","title":{"rendered":"Introduction to Code Analysis in IntelliJ IDEA"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.jetbrains.com\/pages\/static-code-analysis-guide\/\" data-type=\"link\" data-id=\"https:\/\/www.jetbrains.com\/pages\/static-code-analysis-guide\/\" target=\"_blank\" rel=\"noopener\">Static code analysis refers to the practice of scanning code<\/a> for potential problems without actually running the code. Inspections in IntelliJ IDEA can detect potential problems in your project before you compile it. The IDE can highlight various problems, locate dead code, find probable bugs and spelling problems, and improve the overall code structure.<\/p>\n\n\n\n<p>This series of blog posts will cover several ways in which code analysis in IntelliJ IDEA can help you find and fix problems in your code. In this first installment, we\u2019ll start by looking at how the IDE can help you prevent problems while working with code.<\/p>\n\n\n\n<ul>\n<li>Part 2:&nbsp;<a href=\"https:\/\/blog.jetbrains.com\/idea\/2024\/10\/code-analysis-for-your-projects-with-intellij-idea-and-qodana\/\">Code Analysis for Your Projects With IntelliJ IDEA and Qodana<\/a><\/li>\n\n\n\n<li>Part 3: <a href=\"https:\/\/blog.jetbrains.com\/idea\/2024\/11\/advanced-code-analysis-in-intellij-idea\/\" data-type=\"link\" data-id=\"https:\/\/blog.jetbrains.com\/idea\/2024\/11\/advanced-code-analysis-in-intellij-idea\/\" target=\"_blank\" rel=\"noreferrer noopener\">Advanced Code Analysis in IntelliJ IDEA<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Find and fix problems in your code<\/h2>\n\n\n\n<p>When you write code in IntelliJ IDEA, the IDE will let you know if there are areas in your code that need more attention and highlight them for you. Let\u2019s look at some examples.<\/p>\n\n\n\n<p>If your code will not compile, IntelliJ IDEA will let you know \u2013 without requiring you to compile it first. For example, the code below will not compile because <code>FileReader<\/code> may throw a <code>FileNotFoundException<\/code>, which is a checked exception that should be declared in the method. You\u2019ll see that this code is highlighted and when you hover over it, IntelliJ IDEA will tell you what the problem is. It will also suggest how you can fix it \u2013 by adding the exception to the method signature.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"3840\" height=\"2160\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/UnhandledException.png\" alt=\"\" class=\"wp-image-510271\"\/><figcaption class=\"wp-element-caption\">Unhandled Exception<\/figcaption><\/figure>\n\n\n\n<p>IntelliJ IDEA will also let you know when code is redundant. In the following example, <code>new File<\/code> is shown in gray. This is because it is redundant and can be replaced with a file name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"3840\" height=\"2160\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/RedundantCode.png\" alt=\"\" class=\"wp-image-510227\"\/><figcaption class=\"wp-element-caption\">Redundant Code<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Quickly see what\u2019s wrong in your code\u2026<\/h2>\n\n\n\n<p>As you\u2019ve seen above, IntelliJ IDEA will highlight potential problems in your code. If you\u2019d like more information about the problems, you can move your cursor over the highlighted code in the editor, hover over the error stripes in the right gutter, or click the lightbulb icon on the left.<\/p>\n\n\n\n<p>The <em>Inspection <\/em>widget in the top right-hand corner of the editor pane will show you the number of problems in the current file and their severity. You can click the<strong><em> <\/em><\/strong><em>Inspection <\/em>widget to open the list of problems, which will then be displayed in the <em>File<\/em> tab of the <em>Problems<\/em> tool window.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/InspectionWidget.mp4\"><\/video><figcaption class=\"wp-element-caption\">Inspection Widget<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u2026and automatically fix it<\/h2>\n\n\n\n<p>IntelliJ IDEA offers <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/resolving-problems.html\" data-type=\"link\" data-id=\"https:\/\/www.jetbrains.com\/help\/idea\/resolving-problems.html\" target=\"_blank\" rel=\"noreferrer noopener\">quick-fixes<\/a> for these inspections to help you fix your code on the fly. Use <em>\u2325\u23ce <\/em>(macOS) or <em>Alt+Enter<\/em> (Windows\/Linux) to invoke <em>Show Context Actions<\/em> and select the desired action from the list to apply it to your code.<\/p>\n\n\n\n<p>If there are multiple errors and warnings in the code, use <em>F2<\/em> to navigate to the next problem, or <em>Shift+F2<\/em> to navigate to the previous problem. This way, you can fix each problem one by one.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/FixProblems.mp4\"><\/video><figcaption class=\"wp-element-caption\">Fix Problems<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Improve the quality of your code<\/h2>\n\n\n\n<p>Even if there are no problems in your code, IntelliJ IDEA might still have some suggestions on how you can improve your code. Use <em>Show Context Actions<\/em><strong> <\/strong>(<em>\u2325\u23ce<\/em> on macOS or <em>Alt+Enter<\/em> on Windows\/Linux) to apply suggestions to your code. For example, you can try new idioms, like a <code>forEach<\/code> instead of a <code>for<\/code> loop, or a <code>stream<\/code> instead of a <code>while<\/code> loop, or even use a try-with-resources statement instead of closing the <code>BufferedReader<\/code> yourself.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/Suggestions.mp4\"><\/video><figcaption class=\"wp-element-caption\">Suggestions<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Fix all occurrences of a specific problem throughout the file<\/h2>\n\n\n\n<p>If the same problem occurs multiple times in the same file, you don\u2019t need to fix each occurrence individually! IntelliJ IDEA can also fix a particular problem in multiple places in the file. When using <em>Show Context Actions<\/em> (<em>\u2325\u23ce<\/em> on macOS) or <em>Alt+Enter<\/em> on Windows\/Linux), click the three dots to the right of a suggestion to apply that suggestion throughout the file.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2024\/09\/FixAllProblemsInFile.mp4\"><\/video><figcaption class=\"wp-element-caption\">Fix All Problems In File<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>IntelliJ IDEA can help you prevent problems by highlighting code that needs more attention as you\u2019re writing it and help you find areas of improvement for the code you\u2019re working on.&nbsp;<\/p>\n\n\n\n<p>IntelliJ IDEA can also help you resolve problems throughout your project, not just in the file you&#8217;re currently working on. In the <a href=\"https:\/\/blog.jetbrains.com\/idea\/2024\/10\/code-analysis-for-your-projects-with-intellij-idea-and-qodana\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/blog.jetbrains.com\/idea\/2024\/10\/code-analysis-for-your-projects-with-intellij-idea-and-qodana\/\" rel=\"noreferrer noopener\">next part of this series<\/a>, we\u2019ll look at analyzing the code for your project.<\/p>\n\n\n\n<p>Subscribe to our <a href=\"https:\/\/lp.jetbrains.com\/intellij-idea-communication-center\/\" target=\"_blank\" rel=\"noopener\">blog<\/a> if you don\u2019t want to miss it.<\/p>\n","protected":false},"author":1342,"featured_media":510813,"comment_status":"closed","ping_status":"closed","template":"","categories":[4759,5088],"tags":[],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/idea\/510038"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/1342"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=510038"}],"version-history":[{"count":9,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/idea\/510038\/revisions"}],"predecessor-version":[{"id":590368,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/idea\/510038\/revisions\/590368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/510813"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=510038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=510038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=510038"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=510038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}